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.
curl_multi_init
(PHP 5)
curl_multi_init — 新規 cURL マルチハンドルを返す
説明
resource curl_multi_init
( void
)
複数の cURL ハンドルを並列で実行できるようにします。
パラメータ
- mh
-
curl_multi_init() が返す cURL マルチハンドル。
返り値
成功した場合に cURL、失敗した場合に FALSE を返します。
例
例1 curl_multi_init() の例
この例は、ふたつの cURL ハンドルを作成し、それをマルチハンドルに追加して並列実行します。
<?php
// cURL リソースを作成します
$ch1 = curl_init();
$ch2 = curl_init();
// URL およびその他適切なオプションを設定します。
curl_setopt($ch1, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
// マルチ cURL ハンドルを作成します
$mh = curl_multi_init();
// ふたつのハンドルを追加します
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$running=null;
// ハンドルを実行します
do {
curl_multi_exec($mh,$running);
} while ($running > 0);
// ハンドルを閉じます
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>
curl_multi_init
jaisen at jmathai dot com
30-May-2008 04:09
30-May-2008 04:09
ssttoo at gmail dot com
26-Feb-2008 10:57
26-Feb-2008 10:57
An example of how to make parallel POST/GET requests through a reusable function:
http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/
Anonymous
25-Jan-2008 03:09
25-Jan-2008 03: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
18-Dec-2007 03:02
18-Dec-2007 03: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-Jul-2007 11:39
30-Jul-2007 11: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);
