Skip to content

WordPress Http – Read Content From Other Websites

Recently I have been doing a lot of work with API’s and these often involve loading text content (rss/ xml/ json etc) from another website and then displaying the results – for instance the Twitter feed at the bottom of this website uses the Twitter Search API to get my latest updates.

There are all sorts of ways to access this data, but different servers require different methods so it’s good to have a generic method for accessing the data… and WordPress has one built in. Even better; it’s really easy to use.

WordPress Http – WP_Http

Below is a really basic usage for the plugin that will grab the 5 latest items from my Twitter stream and stick them in an array called $content. You can read up on the Twitter search API here.

$url = 'http://search.twitter.com/search.json?q=from%3Abinarymoon&rpp=5';

$request = new WP_Http;
$result = $request->request($url);
$content = array();

if (isset($result->errors)) {
	// display error message of some sort
} else {
	$content = $result['body'];
}

Other places this could be used include (but are not limited to):

  • In my theme The Local I use it to grab the current weaterht for the specified location
  • In Accumulo it’s used to load the RSS feeds that you can see on the homepage
  • Many WordPress plugins use similar functionality to get data for other webservices such as loading Flickr galleries

Is there anything else you can think of that this could be used for?

Ben View All

Ben is a lifelong Nintendo fan who also likes to build websites, and develop games. He also buys way too much Lego.

7 thoughts on “WordPress Http – Read Content From Other Websites Leave a comment

  1. Pingback: Yahoo Weather API
  2. Is this a new feature? I can this of some serious time working with cURL and other functions when using WordPress. Is it in 2.8? Think thats what my site is running … I might be working with an API in my next plugin so this is a really helpful find! Cheers Ben!

    • I think this method has been around for a version or two – before this they used a system called Snoopy which is still available but has been deprecated so could be removed at any time.

  3. Pingback: Follow Friday: Ben Gillbanks - WordPress Hardcore
  4. Pingback: WordPress Caching Tutorial, Custom Caching PHP Library
    • You could always use a regex to parse the returned html and grab the content you want to access. That can be quite complex though so will take some effort to get it working properly. You could also use DomDocument which takes care of a lot of the heavy lifting for you.

Leave a Reply

Your email address will not be published. Required fields are marked *