curl_multi_init

(PHP 5)

curl_multi_initRestituisce un nuovo handle multiplo cURL

Descrizione

resource curl_multi_init ( void )

Permette l'elaborazione di handle multipli cURL in parallelo.

Elenco dei parametri

Questa funzione non contiene parametri.

Valori restituiti

Restituisce una risorsa dell'handle multiplo cURL in caso di successo, FALSE in caso di fallimento.

Esempi

Example #1 Esempio di curl_multi_init()

Questa esempio creerà due handle cURL, li aggiunge ad un handle multiplo, e dopo li eseguirà in parallelo.

<?php
// crea entrambe le risorse cURL
$ch1 curl_init();
$ch2 curl_init();

// imposta l'URL e altre opzioni appropriate
curl_setopt($ch1CURLOPT_URL"http://lxr.php.net/");
curl_setopt($ch1CURLOPT_HEADER0);
curl_setopt($ch2CURLOPT_URL"http://www.php.net/");
curl_setopt($ch2CURLOPT_HEADER0);

//crea l'handle multiplo cURL
$mh curl_multi_init();

//aggiunge i due handle
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$active null;
//esegui gli handle
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);
    }
}

//chiude gli handle
curl_multi_remove_handle($mh$ch1);
curl_multi_remove_handle($mh$ch2);
curl_multi_close($mh);

?>

Vedere anche:

add a note add a note

User Contributed Notes 3 notes

up
8
xxavalanchexx at gmail dot com
5 months 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
-7
hushuilong at gmail dot com
3 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',
)));
?>
up
-15
jaisen at jmathai dot com
6 years ago
http://github.com/jmathai/epicode/tree/master/php/EpiCurl.php

If you fire off 10 curl requests in parallel you don't have to wait for all of them to be finished before accessing one which is already finished.
To Top