How to Achieve Asynchronous cURL Requests in PHP?

Mary-Kate Olsen
Release: 2024-11-03 07:22:02
Original
495 people have browsed it

How to Achieve Asynchronous cURL Requests in PHP?

Asynchronous curl request in PHP

Executing multiple curl requests simultaneously can be a challenge in PHP. In this article, we'll explore different approaches to achieve asynchronous execution using built-in functions and external libraries.

cURL multi-threading

PHP's curl_multi_* functions allow for asynchronous execution of multiple cURL requests. Here's an example:

<code class="php">curl_multi_init();
$mh = curl_multi_init();

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, 'http://example.com/endpoint');
curl_multi_add_handle($mh, $ch1);

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, 'http://example.com/endpoint2');
curl_multi_add_handle($mh, $ch2);

$active = null;
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active &amp;&amp; $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);</code>
Copy after login

pthreads

The pthreads library allows for multi-threaded programming in PHP. Using pthreads, asynchronous curl requests can be achieved like this:

<code class="php">class RequestThread extends Thread {
    public function run() {
        $ch = curl_init();
        // ... set cURL options here

        curl_exec($ch);
        curl_close($ch);
    }
}

$thread = new RequestThread();
$thread->start();</code>
Copy after login

Parallel execution using library

There are also libraries specifically designed for parallel execution in PHP, such as parallel-functions and parallel-request. Here's an example using the parallel-request library:

<code class="php">use Parallel\{Task, Runtime};

$runtime = new Runtime;

$tasks = [
    new Task(function () {
        // ... cURL request 1
    }),
    new Task(function () {
        // ... cURL request 2
    }),
];

$runtime->run($tasks);</code>
Copy after login

Considerations

When executing asynchronous requests, it's important to consider the server's resource limits and potential bottlenecks. It's also crucial to handle errors and exceptions that may occur during execution.

The above is the detailed content of How to Achieve Asynchronous cURL Requests in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template