PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

curl_multi_getcontent> <curl_multi_close
Last updated: Fri, 11 Apr 2008

view this page in

curl_multi_exec

(PHP 5)

curl_multi_exec — Führt die Unter-Verbindungen des cURL-Handles aus

Beschreibung

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

Verarbeitet alle Handles; diese Methode kann unabhängig davon aufgerufen werden, ob eines der Handles Daten lesen oder schreiben muß.

Parameter-Liste

mh

Ein von curl_multi_init() zurückgegebenes cURL-Multihandle.

still_running

Eine Referenz auf ein Flag um festzustellen, ob auf einem oder mehreren Handles noch gearbeitet wird.

Rückgabewerte

Ein cURL-Code der in den cURL Vordefinierten Konstanten definiert ist.

Hinweis: Es werden nur Fehler zurückgegeben, die den Handle-Stack betreffen. Auch wenn CURLM_OK zurückgegeben wird können auf einzelnen Handles Fehler aufgetreten sein.

Beispiele

Beispiel #1 curl_multi_exec() Besipiel

In diesem Beispiel werden zwei cURL-Handles erstellt, einem Mehrfach-Handle hinzugefügt und anschliessend parallel ausgeführt.

<?php
// zwei cURL Resourcen erstellen
$ch1 curl_init();
$ch2 curl_init();

// URL und weitere Optionen setzen
curl_setopt($ch1CURLOPT_URL"http://lxr.php.net/");
curl_setopt($ch1CURLOPT_HEADER0);
curl_setopt($ch2CURLOPT_URL"http://www.php.net/");
curl_setopt($ch2CURLOPT_HEADER0);

// Mehrfach-Handle erstellen
$mh curl_multi_init();

// die zuvor erstellten Handles hinzufügen
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$active null;
// Handles ausführen
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 the handles
curl_multi_remove_handle($mh$ch1);
curl_multi_remove_handle($mh$ch2);
curl_multi_close($mh);

?>



curl_multi_getcontent> <curl_multi_close
Last updated: Fri, 11 Apr 2008
 
add a note add a note User Contributed Notes
curl_multi_exec
viczbk.ru
08-Feb-2008 08:04
http://curl.haxx.se/libcurl/c/libcurl-multi.html

"When you've added the handles you have for the moment (you can still add new ones at any time), you start the transfers by call curl_multi_perform(3).

curl_multi_perform(3) is asynchronous. It will only execute as little as possible and then return back control to your program. It is designed to never block. If it returns CURLM_CALL_MULTI_PERFORM you better call it again soon, as that is a signal that it still has local data to send or remote data to receive."

So it seems the loop in sample script should look this way:

<?php
$running
=null;
//execute the handles
do {
    while (
CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mh, $running));
    if (!
$running) break;
    while ((
$res = curl_multi_select($mh)) === 0) {};
    if (
$res === false) {
        echo
"<h1>select error</h1>";
        break;
    }
} while (
true);
?>

This worked fine (PHP 5.2.5 @ FBSD 6.2) without running non-blocked loop and wasting CPU time.

However this seems to be the only use of curl_multi_select, coz there's no simple way to bind it with other PHP wrappers for select syscall.
robert dot reichel at boboton dot com
20-Oct-2007 05:54
I was testing PHP code provided by dtorop933@gmail.com in curl_multi_exec section of PHP Manual.

The part of the code '$err = curl_error($conn[$i])' should return error message for each cURL session, but it does not.
The function curl_error() works well with the curl_exec(). Is there any other solution for getting session error message with curl_multi_exec() or there is a bug in cURL library.

The script was tested with Windows XP and PHP-5.2.4
shichuanr at gmail dot com
06-Sep-2007 05:12
For people who have problem running the above example script(Example 425. curl_multi_exec()), you can alter it a little bit to make it work properly by replacing the existing chunk of code with the one below:

<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();

// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://www.google.com/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);

//create the multiple cURL handle
$mh = curl_multi_init();

//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$running=null;
//execute the handles
do {
   
curl_multi_exec($mh,$running);
} while (
$running > 0);
//close the handles
curl_multi_remove_handle($mh,$ch1);
curl_multi_remove_handle($mh,$ch1);
curl_multi_close($mh);

?>
substr(&#34;iscampifriese&#34;,1,11) dot &#34; at beer dot com&#34;
04-Jul-2007 06:10
If you are using mulit handles and you wish to re-execute any of them (if they timed out or something), then you need to remove the handles from the mulit-handle and then re-add them in order to get them to re-execute.  Otherwise cURL will just give you back the same results again without actually retrying.

curl_multi_getcontent> <curl_multi_close
Last updated: Fri, 11 Apr 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites