HTTP API in WordPress

As the name suggests, in WordPress, the HTTP API can be used to simplify HTTP requests. It can let you make HTTP requests via PHP, either to the same site or to a different site. But more importantly, HTTP API in WordPress lets you transform URL strings into JSON objects.

Consider the following URL string: http://example.com/wp-json/wp/v2/posts.

It is like any other URL on the Internet. Now, with HTTP API, we can convert it into a JSON object, making use of the wp_remote_get () function from the WordPress core:

$json = wp_remote_get ( 'http://example.com/wp-json/wp/v2/posts' ); 

Now, $json will yield an array, and that is precisely the response that we need.

To understand it better, let us now put together a very small function ...

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.