WordPress includes an exceptionally useful API called the HTTP API that can be used for sending data to and retrieving data from remote APIs. If you are building a plugin that talks to Stripe, MailChimp, or just about any other service that provides an API, you can use the WP HTTP API to make your job significantly easier. For this tutorial, I’m going to show you how to use wp_remote_get(), one of the several functions included in the HTTP API, to retrieve and parse JSON data from a remote API.

There are four primary functions that comprise the HTTP API in WordPress:

For this tutorial, we will use wp_remote_get() since we wish to retrieve data from an API and then parse it.

Most modern APIs return data in a format called JSON (Javascript Object Notation). While the name includes Javascript, you don’t actually have to write or even know Javascript in order to parse JSON. For all intents and purposes, JSON can be considered a mostly universal data format that just nearly all programming languages can read, with PHP being no exception.

A simple JSON object looks like this:

{
   "key":"value"
}

In order to use this in PHP, we need to translate it to a format that PHP understands. To do this, we can use the json_decode() function.

$json  = '{"key":"value"}';

// Translate into an object
$obj   = json_decode( $json );

// Translate into an array
$array = json_decode( $json, true );

It is really that simple, but this is also an exceptionally simple example, so let’s look at a more complex example.

Easy Digital Downloads includes a REST API that can be used for retrieving sales, earnings, customers, discounts, and product data. The first four endpoints require a valid set of API keys for authorization, but the products endpoint of the EDD API is publicly accessible, so we’ll use that as an example.

If you visit https://pippinsplugins.com/edd-api/products in your browser, you will see raw JSON spit out:

Screen Shot 2015-05-19 at 12.56.43 PM

This JSON contains all of the public information about the products I sell on this website. If we wanted to, we could easily use this API endpoint to pull product data from pippinsplugins.com and display it on an external site, perhaps in a sidebar or footer widget.

For this example, let’s assume that we want to pull this product data into a plugin that we have written and then do something with it there, perhaps display it in an admin page.

The first thing we need to do is make the request with wp_remote_get().

$request = wp_remote_get( 'https://pippinsplugins.com/edd-api/products' );

That will make a call to the specified URL and return an array of data about the response, including headers, cookies, body, and more.

Before we do anything with the response body (that’s where the JSON is stored), we need to validate it and ensure that we got back a response that we expected.

If the remote request fails, wp_remote_get() will return a WP_Error, so let’s check if the $request variable is an error:

Once we have determined that the request was not an error, we can proceed to retrieving the data. For this, we’ll use the wp_remote_retrieve_body() helper function:

The $body variable will now contain a JSON object that matches the screenshot above.

All we need to do now is translate the JSON into a format we can read, either an array or an object.

Once we have decoded the JSON, we can interact with the product data just like we would any other object in PHP. For example, we could loop through them and display the products in an unordered list:

Whatever the API, JSON responses are always very similar to work with, so once you understand the methods needed to retrieve and parse the data, working with most APIs becomes significantly easier.

Join now to gain access to this tutorial and more.

Join Now
  1. Ed Fullman

    Pippin,

    Thanks for this post. I happen to be in the middle of trying to leverage the WP-API to create an EDD Download on EDD/FES from an application that is external to our WordPress.

    I see the approach you’ve described for retrieving the publicly available data, and we’ve got that to work another way.

    Is there a way in EDD/FES to create/edit a download? I can move this over to EDD forums if you would like me to.

    • Pippin

      Creating a download product is just like creating a standard WP post (with a post_type of “download”) and then supplying the appropriate meta data.

      Have you familiarized yourself with how to create posts through the WP API?

    • Ed Fullman

      Pippin,

      We have familiarized ourselves about how to create the post. We were going down a route using an external plugin to communicate. However, looking at the EDD-API code and what you describe here, I think we should just focus on leveraging what you’ve built already.

    • Pippin

      The EDD API doesn’t currently support creating products (or other data), it’s only for retrieving data.

  2. Frankie Jarrett

    I recently learned about wp_safe_remote_get() first introduced in 3.6 from Nacin’s talk at Loop Conf this year. I’m wondering if rejecting unsafe URLs should be the more common approach for fetching, much like wp_safe_redirect() has become for redirecting?

    • Pippin

      LoopConf was the first time I had heard of it as well. In general, I’d say “yes”, it’s probably best to use the safe method.

      This was also just pointed on by George Stephanis on Twitter: https://twitter.com/daljo628/status/601410958624448512

Leave a Reply

Error: Please enter a valid email address

Error: Invalid email

Error: Please enter your first name

Error: Please enter your last name

Error: Please enter a username

Error: Please enter a password

Error: Please confirm your password

Error: Password and password confirmation do not match