English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP curl_multi_exec() Function Usage and Example

PHP CURL Reference Manual

(PHP 5)

curl_multi_exec — Run the current CURL handle's sub-connection

Description

int curl_multi_exec ( resource $mh , int &$still_running )

Process each handle in the stack. This method can be called regardless of whether the handle needs to read or write data.

Parameter

mh

CURL multiple handles returned by curl_multi_init().

still_running

A reference used to determine whether the operation is still executing.

Return value

A CURL code defined in the CURL predefined constants.

Note:This function only returns errors related to the entire batch stack. Even if CURLM_OK is returned, there may still be problems with individual transfers.

Online example

This example will create 2 a CURL handle, add them to the batch handle, and then run them in parallel.

<?php
// Create a pair of CURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// Set URL and corresponding options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
// Create batch CURL handle
$mh = curl_multi_init();
// add2handle
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);
$active = null;
// Execute batch handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}
// Close all handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>

PHP CURL Reference Manual