PHP 8.3.4 Released!

curl_multi_init

(PHP 5, PHP 7, PHP 8)

curl_multi_initRetourne un nouveau cURL multiple

Description

curl_multi_init(): CurlMultiHandle

Autorise l'exécution de multiples gestionnaires cURL de façon asynchrone.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

Retourne un gestionnaire cURL multiple.

Historique

Version Description
8.0.0 Cette fonction retourne désormais une instance de CurlMultiHandle; auparavant, une resource était retournée.

Exemples

Exemple #1 Exemple avec curl_multi_init()

Cet exemple crée deux gestionnaires cURL, les ajoute à un gestionnaire multiple, et les exécute de façon asynchrone.

<?php
// Création des ressources cURL
$ch1 = curl_init();
$ch2 = curl_init();

// Définit l'URL ainsi que d'autres 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);

// Création du gestionnaire cURL multiple
$mh = curl_multi_init();

// Ajoute les deux gestionnaires
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

// Exécute le gestionnaire multiple
do {
$status = curl_multi_exec($mh, $active);
if (
$active) {
curl_multi_select($mh);
}
} while (
$active && $status == CURLM_OK);

// Ferme les gestionnaires
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

?>

Voir aussi

add a note

User Contributed Notes 4 notes

up
52
w_haigh at yahoo dot com
8 years ago
Here's an easier to follow example, From : http://arguments.callee.info/2010/02/21/multiple-curl-requests-with-php/

// build the individual requests, but do not execute them
$ch_1 = curl_init('http://webservice.one.com/');
$ch_2 = curl_init('http://webservice.two.com/');
curl_setopt($ch_1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, true);

// build the multi-curl handle, adding both $ch
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch_1);
curl_multi_add_handle($mh, $ch_2);

// execute all queries simultaneously, and continue when all are complete
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

// all of our requests are done, we can now access the results
$response_1 = curl_multi_getcontent($ch_1);
$response_2 = curl_multi_getcontent($ch_2);
echo "$response_1 $response_2"; // output results
up
45
xxavalanchexx at gmail dot com
9 years ago
According to https://bugs.php.net/bug.php?id=61141:

On Windows setups using libcurl version 7.24 or later (which seems to correspond to PHP 5.3.10 or later), you may find that curl_multi_select() always returns -1, causing the example code in the documentation to timeout. This is, apparently, not strictly a bug: according to the libcurl documentation, you should add your own sleep if curl_multi_select returns -1.

Therefore, in order to work correctly on Windows for PHP 5.3.10+, the second loop in the example code should look something like the following:

<?php
while ($active && $mrc == CURLM_OK) {
if (
curl_multi_select($mh) == -1) {
usleep(100);
}
do {
$mrc = curl_multi_exec($mh, $active);
} while (
$mrc == CURLM_CALL_MULTI_PERFORM);
}
?>
up
4
Adam Richardson
6 years ago
One of the URLs used in the first example on this page (lxr.php.net) now gives a proxy error.

If you're using this first script example, replace with a different URL.
up
-16
hushuilong at gmail dot com
12 years ago
Simulate multiple threads request:
<?php
function multiple_threads_request($nodes){
$mh = curl_multi_init();
$curl_array = array();
foreach(
$nodes as $i => $url)
{
$curl_array[$i] = curl_init($url);
curl_setopt($curl_array[$i], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $curl_array[$i]);
}
$running = NULL;
do {
usleep(10000);
curl_multi_exec($mh,$running);
} while(
$running > 0);

$res = array();
foreach(
$nodes as $i => $url)
{
$res[$url] = curl_multi_getcontent($curl_array[$i]);
}

foreach(
$nodes as $i => $url){
curl_multi_remove_handle($mh, $curl_array[$i]);
}
curl_multi_close($mh);
return
$res;
}
print_r(muti_thread_request(array(
'http://www.example.com',
'http://www.example.net',
)));
?>
To Top