Remote Requests with wp_remote_get

If you’re read any of my previous posts on retrieving remote files, then you know that old habits die hard as I often use file_get_contents rather than wp_remote_get.

So, as one does when blogging, I thought it would be helpful to share a simple example of how to use wp_remote_get in your day-to-day work.

wp_remote_get in the WordPress Developer Resources

The post below is merely a snippet of how to use it. That is, it won’t show how to designed, say, a class around it or a UI that allows users to upload a file. But this should be enough to get things started.

And I’ll include how to take the API documentation around it and understand how to leverage it when using a function like this.

Why Use wp_remote_get?

Though I don’t always use the WordPress API (which is usually because, again, old habits carry over or because I’m unaware of a function), I try.

And I firmly believe that if you’re working in an application’s particular environment and said application provides an API for something, then you should use it.

How To Read The Docs

If you’re familiar with how to leverage documentation, then you can skip this section.

With that said, here’s what the documentation around wp_remote_get says:

Retrieve the raw response from the HTTP request using the GET method. Results include HTTP headers and content.

You can read more on both this page in the Codex and this page in the Developer Resources. But before actually using the function, it’s important to understand what it accepts and what it returns.

And the function accepts two parameters:

  1. the URL to which the request is being made,
  2. the set of arguments required for the request (which is outside the scope of this post).

And wp_remote_get returns mixed results based on the outcome of the request. As per the Developer Resources:

(WP_Error|array) The response or WP_Error on failure.

At this point, we know what the function does, what it accepts, and what it returns. So we have enough to go on regarding setting up a simple example.

Using wp_remote_get

With all of the above in place, here’s a simple of example of how to retrieve a file from a given URL. I’m assuming the URL is on your local server (because I don’t want to get into requesting information from other servers in this post) which is why the domain as the TLD of dot-local.

The comments should explain what’s up, but I’ll offer a few more thoughts after that.

The most important things note are as follows:

  1. Check the headers to make sure they are set. If not, then there’s no need to proceed with grabbing the body of the file.
  2. If there is an error, then we need to handle that gracefully.
  3. If all else checks out, then we’re ready to write the file to disk. I’m using PHP’s file functions to do this. It’s a little outside the scope of the post but should be easy to understand if you study the links below.

Ultimately, using wp_remote_get affords you a lot of functionality wrapped in an easy-to-use function.

Some PHP Resources