Skip to content

Instantly share code, notes, and snippets.

@nissuk
Created May 13, 2012 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nissuk/2688708 to your computer and use it in GitHub Desktop.
Save nissuk/2688708 to your computer and use it in GitHub Desktop.
<?php
// cURL リソースを作成します
$ch1 = curl_init();
$ch2 = curl_init();
// URL およびその他適切なオプションを設定します。
// (※ URLはCURLOPT_URLでなくても curl_init($url) に入れてもいい)
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);
// マルチ cURL ハンドルを作成します
$mh = curl_multi_init();
// ふたつのハンドルを追加します
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$active = null;
// ハンドルを実行します
// (※ マルチハンドル開始。最低1回で終わる
// ※ do - while単位で1つの実行単位になっていて curl_multi_select() を初回スキップするために
// ループが分割されている)
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
// ※ $activeは curl_multi_exec() が処理中のとき1, 終了のとき0に書き換える(参照)
// curl_multi_select() は次のactivityまで待つ
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
// ハンドルを閉じます
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment