WordPress Http API – 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.

If you’re not using WordPress then you may find that using cURL or file_get_contents would be good alternatives to the WordPress HTTP API.

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

How was it for you? Let me know on BlueSky or Mastodon

(Please) Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

13 May 2010

6 Tips to Build Better WordPress Themes

If you want to make WordPress themes, for clients, to release for free or to sell, then there are a lot of factors you need to take into consideration. Below are some hints and tips that should help ease your...
17 Oct 2012

WordPress Social Network Aggregation

I really like the idea of a Tumblog – and even have one on Tumblr.com – but I don’t promote it anywhere. Conceptually it’s great – but I don’t like not having control over my content.What I would really like...
22 Nov 2014

WordPress 4.1 Improvements for Theme Developers

WordPress 4.1 is bringing with it a couple of cool new additions for theme developers. They’re things that are currently a bit messy to implement in themes. For me they are things that I do the same way in all...
27 May 2013

WordPress: 10 Years Young, What Does The Future Hold?

WordPress is now 10 years old. I started using wordpress 9 years ago – which means I joined the WordPress community early on. The reason I chose WordPress is simply because of the fabled 5 minute install process – I...
29 Mar 2009

The future of WordPress themes

A couple of weeks ago there was quite a lot of talk within the WordPress themes community about the future of WordPress. Ian Stewart started it, and then it spread around the blogosphere… so I thought I’d offer my rather...