Ben
Ben is a lifelong Nintendo fan who likes to build websites, and make video games. He buys way too much Lego.
WordPress and Games
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.
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):
Is there anything else you can think of that this could be used for?
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.
Can I get a certain section from the content? like only the content block?
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.