WordPress already created using PHP, so why do we need to make use of WP REST API with PHP? There are many reasons for that, including but not limited to:
- A custom CMS solution specifically for a small group or company
- A simple CMS using third party PHP libraries, such as CakePHP, or a custom coded library
- A simple PHP web app
We can use WordPress admin as the main settings area, and use WP REST API to apply the changes.
It even can use as a WordPress theme to ease the theme development process. The first of several advantages comes to mind when using the API in a theme is the ability to use WordPress built-in functions. This makes design and development processes even easier by integrating WordPress features, such as Customizer and Widgets, into the theme without using large chunks of code (loop) to output the data from the database.
In fact, most of the functions we will be talking about in this post are WordPress functions. These functions are called WordPress HTTP API and are used to retrieve the content from outside domains. You can view their source code on the WordPress Trac. All of them use curl related PHP functions instead of simple file_get_contents().
wp_remote_get() is one of two main functions in WordPress that is used to get information from an outside domain. Another function is wp_remote_post(), which is equivalent to submitting a form on a website, is a topic for another post.
The example below shows the usage of this function:
$response = wp_remote_get(“http://blog.robbychen.com/wp-json/posts”);
Make sure to replace the domain with yours.
wp_remote_retrieve_headers() is the same as the $response[‘headers’] value from wp_remote_get() above. This is very useful compared to file_get_contents() because the json file from the WP REST API only shows the details of every post, not the overall details of all the posts, such as the total number of posts and how many pages there are if we use “posts_per_page” filter.
Although these can be calculated using a little script, however it’s not a good way to do it. Imagine there are thousands of posts on the site, using “posts_per_page=-1” filter and loop across all of the posts can slow down the site significantly, not to mention the server can eventually freeze, causing you to restart the server.
Here is the sample usage for this function:
$headers = wp_remote_retrieve_headers($response);
It returns an array containing all of the headers from the GET response. For example, $headers[‘x-wp-total’] and $headers[‘x-wp-totalpages’] are the total number of posts and the total pages number based on the “posts_per_page” filter respectively.
wp_remote_retrieve_body() is the same as the $response[‘body’] from wp_remote_get() function above. It’s basically like file_get_contents(), outputs the entire content of the target URL. For example,
$body = wp_remote_retrieve_body($response);
outputs the json document from /wp-json/posts of this blog. You can then use json_decode PHP function to convert the retrieved json document to a PHP object and use it within your project.
Although using PHP to process WP REST API has above advantages, there are some disadvantages as well. Its main disadvantage is the loading speed for your pages.
When both WordPress and API-consume pages use PHP or other Server-Side Language on the same server, your web server uses large amount of memories and processing power to interpret the pages and output them to the browser. Thus, it may become slow or unresponsive depending on the hardware configuration of your server. For this reason alone, I recommend you to either consume WP REST API from a different hosting server, or use AJAX to consume the API. There are many tutorials out there on how to use various third-party JS libraries with WP REST API. There is even a NPM module for Node.js.