How Can I Execute cURL Requests Asynchronously in PHP?

Barbara Streisand
Release: 2024-11-01 11:16:30
Original
358 people have browsed it

How Can I Execute cURL Requests Asynchronously in PHP?

Async Curl Request in PHP

Executing curl post requests asynchronously in PHP can enhance performance and prevent potential delays. Here's how you can achieve this using different methods:

Using Async cURL Functions

When using curl_multi_*, you can execute multiple cURL requests simultaneously. Here's an example code:

<code class="php">$curl = curl_init($request);
curl_setopt($curl, CURLOPT_URL, $url_onfleet);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $api_onfleet);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");  
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, '{&quot;destination&quot;:{&quot;address&quot;:{&quot;unparsed&quot;:&quot;'.$pickup_address.'&quot;},&quot;notes&quot;:&quot;'.$comments.'&quot;},&quot;recipients&quot;:[{&quot;name&quot;:&quot;'.$name.'&quot;,&quot;phone&quot;:&quot;+61'.$phone.'&quot;,&quot;notes&quot;:&quot;Number of riders: '.$riders.'&quot;}],&quot;completeBefore&quot;:'.$timestamp.',&quot;pickupTask&quot;:&quot;yes&quot;,&quot;autoAssign&quot;:{&quot;mode&quot;:&quot;distance&quot;}}');

$mh = curl_multi_init();
curl_multi_add_handle($mh,$curl);

$active = null;
//execute the handles
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);
    }
}
//close the handles
curl_multi_remove_handle($mh, $curl);
curl_multi_close($mh);</code>
Copy after login

Using pThreads

pThreads is a threading library for PHP. Here's how you can use it for async curl requests:

<code class="php">class Request1 extends Thread {
    public function run() {
        // Execute the first cURL request here
    }
}

class Request2 extends Thread {
    public function run() {
        // Execute the second cURL request here
    }
}

$req1 = new Request1();
$req1->start();
$req2 = new Request2();
$req2->start();</code>
Copy after login

Both methods allow asynchronous execution of curl requests, enabling better performance and efficient handling of parallel tasks.

The above is the detailed content of How Can I Execute cURL Requests Asynchronously 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!