PHP Doku:: Gibt einen cURL-Multi-Handle zurück - function.curl-multi-init.html

Verlauf / Chronik / History: (50) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DiensteCURLcURL Funktionencurl_multi_init

Ein Service von Reinhard Neidl - Webprogrammierung.

cURL Funktionen

<<curl_multi_info_read

curl_multi_remove_handle>>

curl_multi_init

(PHP 5)

curl_multi_initGibt einen cURL-Multi-Handle zurück

Beschreibung

resource curl_multi_init ( void )

Erlaubt die parallele Verarbeitung mehrerer cURL-Handles.

Parameter-Liste

mh

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

Rückgabewerte

Gibt im Erfolgsfall ein cURL-Handle zurück, andernfalls FALSE.

Beispiele

Beispiel #1 curl_multi_init()-Beispiel

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

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

// URL und weitere Optionen setzen
curl_setopt($ch1CURLOPT_URL"http://www.example.com/");
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);

$running=null;
// Handles ausführen
do {
    
usleep(10000);
    
curl_multi_exec($mh,$running);
} while (
$running 0);

// Handles schliessen
curl_multi_remove_handle($mh$ch1);
curl_multi_remove_handle($mh$ch2);
curl_multi_close($mh);

?>

Siehe auch


4 BenutzerBeiträge:
- Beiträge aktualisieren...
jaisen at jmathai dot com
30.05.2008 9:09
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.
Anonymous
25.01.2008 7:09
In the example above, rather than busy looping, curl_multi_select() should be used.  The call isn't adequately described in the php documentation, so you need to look at the libcurl-multi man page.  curl_multi_fdset() on this page is  curl_multi_select() here, and curl_multi_perform() is curl_multi_exec() here.  Read on and write better code.
php at twobears dot cz
17.12.2007 19:02
It's not good to use plain while cycle like in the example because you are going to consume all the cpu time just by checking if you are not done. E.g. using usleep(50) in the middle of while could solve that gracefully...
snoyes at gmail dot com
30.07.2007 16:39
In the example shown, the calls to curl_multi_remove_handle() should include the resource as the first parameter:

curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)