A Note on WP_Http and HTTP Requests in WordPress

We all know and love the How To Make HTTP Requests with WordPress article by @Ozh written back in 2009, and I had quite an interesting discussion in the WordPress IRC channel today, mainly about WP_Http and how the 302 and 301 redirects are handled through different transports. The initial question was about grabbing the final URL to which request was redirected to, turns out there is no good way of grabbing that.

But an interesting thing came up by @sivel (modified and formatted):

In general we recommend not interacting with the class directly but using the wrappers, but in any case, there is no way to grab the final efective URL. And the reason behind that is the class is lazy loaded, so that we are not consuming additional memory when we don’t need the class. We only include the functions and then lazy load the class if needed. … I would rely on what is always going to be included and not have to worry about including a file manually.

So it turns out that it’s best to use the wrapper functions, which are:

And some others defined in the wp-includes/http.php file.

So taking that into account, here are Ozh’s examples rewritten (and slightly modified) using the HTTP wrapper functions:

Simple HTTP GET Request

 $result = wp_remote_get( 'http://search.twitter.com/search.json?q=rabbits' );
 $json = json_encode( $result['body'] );
 print_r( $json );

Assuming that json_encode is available on your PHP installation.

Simple HTTP POST Request

 $url = 'http://your.api/endpoint';
 $post_data = array( 'name' => 'Konstantin' );
 $result = wp_remote_post( $url, array( 'body' => $post_data ) );

Check the $result variable for possible errors or output from your API.

Basic Authentication

 $url = 'http://your.api/endpoint';
 $username = 'your-username';
 $password = 'your-password';
 $headers = array( 'Authorization' => 'Basic ' . base64_encode( "$username:$password" ) );
 $result = wp_remote_get( $url, array( 'headers' => $headers ) );

Or use wp_remote_post and include the $post_data array if you’re firing a POST request with basic auth. Don’t forget to change the URLs used above to something more realistic, your.api doesn’t really exist (or does it?). A good place to find APIs to play around with is ProgrammableWeb.

So that’s about it! Doesn’t solve my redirect question though, but was definitely worth a try ;) Thank you for reading and of course retweeting!

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.

2 comments

  • When I use this code, it outputs yes, so class does exists. Didn't get your lazy load point.

    <pre><code>require('wp-load.php');

    if ( class_exists( 'WP_Http' ) )
    echo "yeah";
    else
    echo "boo";
    </code></pre>

  • Great post Konstantin. It's nice too see more and more information about HTTP API. I think you should mention wp_remote_retrieve_body()