How to Send Asynchronous Requests with cURL in PHP
Jul
09

How to Send Asynchronous Requests with cURL in PHP

In our previous blog, we explored how Guzzle's asynchronous functionality can streamline your application's performance. But what about good old-fashioned synchronous requests? Fear not, cURL provides a powerful alternative for scenarios where asynchronous operations aren't necessary.

Setting Up Your PHP Environment

Before diving into examples, ensure cURL is enabled in your PHP environment. You can check this by looking for the cURL section in your phpinfo() output or running php -m | grep curl in your terminal.

Let's start with a basic example of making a synchronous GET request using cURL in PHP.The API endpoint is the same as provided in the previous article. It takes 5 seconds to respond.

Making Synchronous Requests with cURL:

Here's how to make synchronous requests using cURL:

  • We have an array of URLs we want to request.
  • We initialize a multi handle using curl_multi_init.
  • We loop through the URLs, creating and configuring a cURL handle for each one, and add each handle to the multi handle.
  • We execute the requests using curl_multi_exec and curl_multi_select in a loop until all requests are complete.
  • We retrieve the responses using curl_multi_getcontent, then remove and close each handle.
  • Finally, we close the multi handle and print the responses.
    $time_start = microtime(true);
    $urls = [
        url('api/slow-url'),
        url('api/slow-url'),
        url('api/slow-url'),
        url('api/slow-url'),
        url('api/slow-url'),
    ];
    $mh = curl_multi_init();
    $curlHandles = [];
    foreach ($urls as $i => $url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_multi_add_handle($mh, $ch);
        $curlHandles[$i] = $ch;
    }
    $running = null;
    do {
        curl_multi_exec($mh, $running);
        curl_multi_select($mh);
    } while ($running > 0);
    $responses = [];
    foreach ($curlHandles as $i => $ch) {
        $responses[$i] = curl_multi_getcontent($ch);
        curl_multi_remove_handle($mh, $ch);
        curl_close($ch);
    }
    curl_multi_close($mh);
    foreach ($responses as $response) {
        dump( $response);
    }
    $time_end = microtime(true);
    $execution_time = ($time_end - $time_start);
    dd("Total Execution Time of async-request: {$execution_time} seconds");

Performance Comparison with Asynchronous Requests

Let's compare the performance of asynchronous cURL requests with our previous example of asynchronous requests using Guzzle. As we know API endpoints takes 5 seconds to respond. In synchronous execution, if we make five requests sequentially, the total execution time would be the sum of individual request times (25 seconds). This contrasts with asynchronous execution, where all requests can complete concurrently in approximately 5 seconds, as demonstrated earlier and we can also see in the following screenshot.

 

async-requests-in-curl

Conclusion

In this post, we explored how to perform asynchronous HTTP requests using cURL in PHP. which run concurrently and provide non-blocking behavior, synchronous requests offer simplicity and direct control over the request-response cycle.

Stay tuned for more insights into web development tools and techniques on Dailyusewebtools.com!

Also Read : How To Send Asynchronous Requests with Guzzle HTTP Client

 

Contact

Get in touch with us

Feel free to request missing tools or give some feedback.

Contact Us