DEV Community

Cover image for WP Snippet #007 Get and Post to remote Api with Php.
Stephan Nijman
Stephan Nijman

Posted on • Originally published at

WP Snippet #007 Get and Post to remote Api with Php.

Originally posted on my website on February 27th 2020

How to make Get and Post requests to a remote Api with WordPress Php

Sometimes you may want to make requests to a remote/external Api to get some data. Maybe you want to show your latest tweets on your blog, or maybe you want to get the latest posts from a different WordPress site. For these cases WordPress has the wp_remote_get and wp_remote_post Php functions.

Make a Get request.

In this snippet we create a new function called do_remote_get that excepts one parameter called $url that must be of type string. Inside our new function we use the wp_remote_get function to make the actual http request. The wp_remote_get function accepts two parameters being:

  • $url (String): The Remote url/endpoint to call. In this case we pass it the $url variable that is passed to the do_remote_get function.
  • $args (Array): An array of arguments for the request. This array can have many arguments, but in our case we just use two. The httpversion to use, and we set blocking to true, meaning the calling code requires the result of the request.

When the request is done, we pass the $response to a function called wp_remote_retrieve_body. This function checks if the response is not an WP_Error object and has a valid "Body". If it does it will return the response body. If not it will return a empty string.

We then pass the output to the json_decode function to decode the returned Json data. Now remember that the return value from the wp_remote_retrieve_body function can be an empty string making json_decode return a falsy value. That is why we use the ternary operator ?: [] at the end to make sure that we always return an array.

We can now make a get request to an Api like shown below:

In this example we use our new do_remote_get function to make a Get request to the JSONPlaceholder Api and fetch some (fake) posts. We then loop over the posts and echo out their titles.

Note: In this example we got back a array of objects from our do_remote_get function. If you want the objects to be associative arrays as wel you can pass true as the seccond parameter to the json_decode function.

Make a Post request.

In the example above we used wp_remote_get to fetch some posts from a remote Api. Next we will tackle Post requests to create a post on the remote Api.

For Post request we create a new function called do_remote_post which is similar to the do_remote_get function, but excepts a second parameter $data that holds the data to send to the remote Api.

Within the do_remote_post function we now use the wp_remote_post function to make the request. The wp_remote_post function accepts the same parameters as its wp_remote_get counterpart. For the arguments array we pass a extra argument body and pass it the $data array variable.

We can now make a post request to create a new post on the Api like shown below:

Here we use the do_remote_post function to make a post request to the JSONPlaceholder Api, passing it the url/endpoint and an array representing the post we want to create.

Finally we var_dump the response from the Api. The JSONPlaceholder Api will simply return a Json object of the post we created.

Note: Api request take time to resolve, and should preferably be cached to speed up page load. In the upcoming snippet/article we will discuss WordPress Transients which can be used to cache Api requests.


Found this post helpful? Follow me on twitter @Vanaf1979 or here on @Vanaf1979 to be notified about new articles, and other WordPress development related resources.

Thanks for reading

Top comments (0)