PHP: Perform HTTP requests in parallel

Ever had to request multiple HTTP-resources in your web application? Often, you need data from one request to be able to request the second – in this case there is little you can do but wait for the first to return. However, if the requests are not dependent on each other, you can use a pretty cool trick: curl_multi_*.

Let’s say you wanted to fetch the public data for VG.no and tech.vg.no from Facebook’s Graph API. One might use file_get_contents, or a standard curl-call:

The problem with this approach is obviously that it waits for each request to return before proceeding to the next. If each request takes 5 seconds to perform, we’d have to wait 10 seconds before we’d be able to process all our data and return it to the user.

So, how do we make these request perform in parallel? curl_multi_*. This feature seems to have been introduced in PHP5, but I’m sure many (like me) have not come across it before. Let’s take a look at how we could optimize our code using these functions:

Using this technique, we’ve reduced the time it takes to perform the requests down to the slowest request in the batch. Nice!

However, I’m sure many (like me) are looking at this code and thinking: “Wow. That is a lot of code, and far from readable”. I agree. Thanks to a wonderful PHP HTTP client called Guzzle, we can achieve the same result with much more readable code:

I’ve put together a simple demo repository showing the difference in speed between these approaches. It’s available on Github for anyone who wants to take a look.

Happy HTTP’ing!

Read more from the Software engineering category
SUBSCRIBE TO OUR UPDATES
Menu