官术网_书友最值得收藏!

Interacting REST API via PHP

Let us now learn how to copy posts from one site to another site. Basically, we will be using REST API to GET JSON post data and convert it into a PHP object. In the previous section, we have already created a function for this and we will reuse the code. It is as simple as passing the following request:

$url = 'http://example.com/wp-json/wp/v2/posts/1'; 
$post = get_json( $url ); 

Now, to create a copy of the post, we just have to turn $post into an array and then pass it to wp_insert_post().

To convert $post, we will follow a standard web development practice. Before the code, let us first spend some time understanding what we need to do.

Explanation of function

As already stated, we have to ensure that the data being passed to the function is an array. If the data is an object, we will need to typecast it as an array because the rest of the function will require an array.

Thereafter, once we have an array, we will setup a second array because we will be converting or copying the array keys. For instance, we will copy values from array key X to array key post_X and then unset the old key values.

Lastly, we just need to pass the array to wp_insert_post(). This function will create a new post for us and also return the ID of the new post.

Now that we know what we are trying to do, let us put it in practice. The following is what the code should look like:

function insert_post_from_json( $post ) { 
//checking to see if array or object; because we can work only with array 
if ( is_array( $post ) || ( is_object( $post ) && ! is_wp_error( $post ) ) ) { 
//ensure $post is an array; or force typecast 
$post = (array) $post; 
} 
else { 
return sprintf( '%1s must be an object or array', __FUNCTION__ ); 
} 
//Create new array for conversion 
//Set ID as post_id to try and use the same ID; note that leaving ID as ID would //UPDATE an existing post of the same ID 
$convert_keys = array( 
     'title' => 'post_title', 
     'content' => 'post_content', 
     'slug' => 'post_name', 
     'status' => 'post_status', 
     'parent' => 'post_parent', 
     'excerpt' => 'post_excerpt', 
     'date' => 'post_date', 
     'type' => 'post_type', 
     'ID' => 'post_id', 
); 
//copy API response and unset old key 
foreach ( $convert_keys as $from => $to ) { 
if ( isset( $post[ $from ] ) ) { 
    $post[ $to ] = $post[ $from ]; 
    unset( $post[ $from ] ); 
} 
} 
//remove all keys of $post that are disallowed and convert objects (if any) to strings 
$allowed = array_values( $convert_keys ); 
foreach( $post as $key => $value ) { 
if( ! in_array( $key, $allowed ) ) { 
unset( $post[ $key ] ); 
} else{ 
if ( is_object( $value ) ) { 
$post[ $key ] = $value->rendered; 
} 
} 
} 
//All done. Create post and return its ID 
return wp_insert_post( $post ); 
} 

The output of our function will result in copying a post from a remote site to our site. You can see the new post, thereafter, on your WordPress site, as shown in the following screenshot:

主站蜘蛛池模板: 万源市| 百色市| 堆龙德庆县| 镇沅| 黑龙江省| 大邑县| 开远市| 剑河县| 石棉县| 禹州市| 兰溪市| 遂昌县| 鸡东县| 临城县| 保亭| 汨罗市| 阜宁县| 时尚| 敦煌市| 邵阳市| 凉城县| 新乡市| 平利县| 衡南县| 久治县| 邻水| 武川县| 仁化县| 开封市| 高唐县| 鸡泽县| 新河县| 武鸣县| 武强县| 中卫市| 离岛区| 铜梁县| 临江市| 沭阳县| 乌鲁木齐市| 正蓝旗|