WP Snippet #007 Get and Post to remote Api with Php.
Tags: wordpress, webdev, javascript
Published: 2020-02-27 19:31:50

---
title: WP Snippet #007 Get and Post to remote Api with Php.
published: true
description: In this code snippet we will discuss the WordPress wp_remote_get and wp_remote_post functions that can be used to fetch data from a remote Api using Php
canonical_url: https://since1979.dev/snippet-007-get-and-post-to-remote-api-with-php/
cover_image: https://since1979.dev/wp-content/uploads/2020/02/snippet-007-get-and-post-to-remote-api.jpg
tags: wordpress, webdev, javascript
---
[Originally posted on my website on February 27th 2020](https://since1979.dev/snippet-007-get-and-post-to-remote-api-with-php/)

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.

{% gist https://gist.github.com/vanaf1979/3cd608b2e887c19e6843b3f2823785c2 %}

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](https://developer.wordpress.org/reference/functions/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](https://developer.wordpress.org/reference/classes/WP_Http/request/), 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](https://developer.wordpress.org/reference/functions/wp_remote_retrieve_body/)*. This function checks if the response is not an [*WP_Error*](https://developer.wordpress.org/reference/classes/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](https://www.php.net/manual/en/function.json-decode.php)* 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](https://www.codementor.io/@sayantinideb/ternary-operator-in-php-how-to-use-the-php-ternary-operator-x0ubd3po6) *?: []* 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:

{% gist https://gist.github.com/vanaf1979/8eb244d9c87f0a79fa0d337ebaa9f70b %}

In this example we use our new *do_remote_get* function to make a Get request to the [JSONPlaceholder](https://jsonplaceholder.typicode.com/) 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.

{% gist https://gist.github.com/vanaf1979/16ee07a9c9b6ad91bd39e1b8b6321e06 %}

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

{% gist https://gist.github.com/vanaf1979/c2b17d53b600ddbe8baeec6010258e65 %}

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](https://developer.wordpress.org/reference/functions/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:

{% gist https://gist.github.com/vanaf1979/73e95079fbf44edb906aa4e5e9af2c7d %}

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](https://www.php.net/manual/en/function.var-dump.php)* 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](https://developer.wordpress.org/apis/handbook/transients/) which can be used to cache Api requests.

#### Follow
Found this post helpful? Follow me on twitter [@Vanaf1979](https://twitter.com/Vanaf1979) or here on Dev.to [@Vanaf1979](https://dev.to/vanaf1979) to be notified about new articles, and other WordPress development related resources.

**Thanks for reading**