我创建了一个代码,从远程API获取数据,然后回显某些数据&;将这些数据导入post type 使用wp_insert_post()
&;post meta**使用wpdb->query()
.
这是我当前的代码:
$imdbid = $_GET[\'id\'];
$string = file_get_contents("http://www.omdbapi.com/?i=".$imdbid."&apikey=API_KEY_HEREKEYVALUE");
$json = json_decode($string, true);
if( $json[\'Response\'] === "True" ){
//METADATA
$dataID = $json[\'imdbID\'];
$dataTitle = $json[\'Title\'];
$dataYear = $json[\'Year\'];
$dbdata = array(
\'post_title\' => $dataID,
\'post_status\' => \'publish\',
\'post_type\' => \'datadb\',
\'post_author\' => 1
);
if( $dbid = wp_insert_post( $dbdata ) ){
//METADATA PRINT
echo $dataID;
echo $dataTitle;
echo $dataYear;
$statement = "INSERT INTO wp_postmeta
(post_id, meta_key, meta_value)
VALUES
(\'".$dbID."\', \'ztdb_imdb_id\', \'%s\'),
(\'".$dbID."\', \'ztdb_title\', \'%s\'),
(\'".$dbID."\', \'ztdb_year\', \'%s\');";
$sql = $wpdb->prepare($statement, $dataID, $dataTitle, $dataYear);
$wpdb->query($sql);
}
}
现在,如果我只使用这些代码,页面将不会停止加载,直到
wp_insert_post()
和
wpdb
已完成。是否有任何方法可以在后台运行这两个程序,以便在访问页面和
$dataID, $dataTitle, and $dataYear 是否立即回声/显示?