Concurrent HTTP requests in PHP using pecl_http

The pecl_http extension has a little gem that can be handy at times – HttpRequestPool. Using this, you can send concurrent HTTP requests and can gain efficiency in fetching non-related data at once. For example, from an external source if your application needs to retrieve an user’s profile, their order history and current balance, you can send parallel requests to the API and get everything together.

Here is a simple example code to illustrate that:

  • ali asif

    how it is different then curl_multi ?

    • Mohammad Emran Hasan

      Technically its not different and the extension might use curl_multi internally (not sure though, read somewhere).

      However, I believe the api of this extension is much cleaner and focused as a http client. The curl extension requires a lot of boilerplate code and referring the documentation for finding the nitty-gritty options.