PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Streams

Note: This article was originally published at Planet PHP on 13 December 2010.
Planet PHP

If you want to start building your own apps, one of the most important things you need to know is how to access APIs. How else are you going to get all that awesome data people are posting on the Web?

When it comes to making HTTP requests with PHP, you have several options, the most popular three being cURL, pecl_http, and streams. I generally use streams, and I'm choosing to highlight this method here, because the HTTP streams wrapper is native to PHP. This means you can dive right in without worrying about whether your installation of PHP has all the right libraries. I also really like that the streams interface is very straightforward and consistent across different request types. This means that once you know how to do an HTTP request, it's a simple matter of changing a few settings and you're on your way to doing whatever type of request you want. I've also found that a lot of people aren't very familiar with this feature, so I wanted to make sure it was part of our PHP adventure!

I've chosen to highlight the Flickr API, because it's quick and easy to get an API key, and it's a great example of an API done right. Flickr has created very thorough and well-written API documentation, and there's a lot of potential for making cool stuff with it right away. For our purposesa-aaccessing data from Flickra-awe'll only need to do a basic GET request. This is good, because you can start doing fun stuff with the API really quickly using the same basic streams setup for each request.

So, let's get to work! If you follow along here, you'll have a quick Flickr search and display app in no time at all.

The first thing you need is an API key. Go through the request process, and you'll receive a key immediately upon filling out the form.

Next, you'll want to check out the Flickr API docs and decide which methods you'd like to try. To start, I recommend doing a simple search.

Once you've chosen a method, perform a test to make sure you have a URL that returns the data you want. To do this, select an API method to work with, put together a URL to access it, and paste it right in your browser. For example, to search for photos, the API method we'll use is called flickr.photos.search. It requires that you include the parameters api_key and text. We should also limit the number of results we get back, just in case, with per_page. We know from the documentation that the base URL is http://api.flickr.com/services/rest/, so we can construct the full URL by appending the necessary parameters. For example: http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=[apikey]&text=brooklynbeta&per_page=5

Now that we've got a working URL, let's write a simple PHP script to do a GET request with streams and pull back some XML. Here's the code we need:

array('method' = "GET",)); // Open up a stream! $context = stream_context_create($opts); // Send a request to $url, and store the response. $result = file_get_contents($url, FALSE, $context); ?

Here's a example of the XML that will be returned:

You can use SimpleXML to parse the XML you get back, and a simple modification to $opts is all you need to be able to send POST requests and all sorts of other fun stuff. For simple GET requests, however, you don't necessarily need streams; SimpleXML can both fetch and parse the data. Let's try that now, and we can also add cod

Truncated by Planet PHP, read more at the original (another 3699 bytes)