WordPress HTTP API
Introduction
WordPress HTTP API allows you to either GET or POST something from or to other sites outside of your WordPress site.
Making HTTP requests in PHP is not difficult, and a variety of methods exists: using fopen(), using cURL extension, using file streams with fsockopen() and fwrite() for instance. The problem is: depending on server setup, this might or might not work on another server. The good new is: once again, WordPress has a no-brainer API that makes it dead easy and compatible with all environments.
WordPress, since version 2.7 and thanks to the extreme coding leetness of Jacob Santos, has a nifty class that sits in your wp-includes directory, in file http.php: WP_Http.
HTTP Class
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
The power and beauty of this class is that, depending on what’s available on the server, it will pick up the best method to perform HTTP request. Don’t bother checking for the presence of HTTP extension, fopen() behavior, existence of curl_init(), as this class will do all this for you.
As we mentioned above, WordPress has a class that simplifies making HTTP calls. The class name is “WP_Http” and the concerned method is “$wp_http_instance->request($url)“. This method returns the result of the HTTP request which we’ll detail later.
if( !class_exists( 'WP_Http' ) ) {
include_once( ABSPATH . WPINC. '/class-http.php' );
}
$request = new WP_Http;
$args = array ();
$result = $request->request( 'http://mysite.com' , $args );
Since this bit of code is a little long (and repetitive if you do many requests), WordPress provides a simpler function which does exactly the same thing:
$arg = array ();
wp_remote_request ('http://mysite.com', $arg);
wp_remote_request() – Retrieves a URL using either the default GET or a custom HTTP method (should be caps) that you specify.
Request Method
In the arguments array, you can specify one or more of the request parameters. One of these parameters is the request method. If you are making a “GET” request:
$arg = array ( 'method' => 'GET');
wp_remote_request ( 'http://mysite.com' , $arg );
It goes even further with 3 additional helper functions for the GET, POST and HEAD request methods since they are used most:
wp_remote_get() – Retrieves a URL using the GET HTTP method
wp_remote_post() – Retrieves a URL using the POST HTTP method
wp_remote_head() – Retrieves a URL using the HEAD HTTP method
GET Method
wp_remote_get() – Retrieves a URL using the GET HTTP method
$resp = wp_remote_get( 'http://example.com/' );
$body = $resp['body'];
// perform action with the content.
POST Method
If you want to post some data to a remote URL using the WordPress HTTP API, do the following:
$args = array(
'body' => 'some data',
'user-agent' => 'Yoast Coolness'
);
$resp = wp_remote_post( 'http://example.com', $args );
You can then handle $resp in the same way as with wp_remote_get. Both functions are actually wrapper functions on top of the function request in class WP_Http, see the XRef for that function for more details.
http://xref.yoast.com/trunk/nav.html?_functions/request.html
PLUGIN EXAMPLE
http://wp.tutsplus.com/tutorials/creative-coding/a-look-at-the-wordpress-http-api-a-practical-example-of-wp_remote_get/