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 ...

Get Learning WordPress REST API now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.