Created
March 6, 2014 09:34
-
-
Save yuktse/9386156 to your computer and use it in GitHub Desktop.
multi curl and single curl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
set_time_limit(0); | |
include 'runtime.php'; | |
/** | |
* 使用多个子连接读同时读取多个连接 | |
*/ | |
function multiCurl($urls) | |
{ | |
// 构建 $multi_handle | |
$multi_handle=curl_multi_init(); | |
foreach ($urls as $i => $url) | |
{ | |
$conn[$i]=curl_init($url); | |
curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1); | |
$timeout = 3; | |
curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT, $timeout); | |
curl_setopt($conn[$i], CURLOPT_FOLLOWLOCATION, 1); | |
curl_multi_add_handle($multi_handle,$conn[$i]); | |
} | |
// 连接,下面这样三个循环的写法可以减少CPU占用 | |
$active = false; // 是否还有子连接在进行中。 | |
do { | |
$mrc = curl_multi_exec($multi_handle,$active); // 运行所有子连接。 | |
} while ($mrc == CURLM_CALL_MULTI_PERFORM); //当正在接受数据时 | |
while ($active and $mrc == CURLM_OK) { | |
if (curl_multi_select($multi_handle) != -1) { // curl_multi_select会一直等待下一个可用连接 | |
do { | |
$mrc = curl_multi_exec($multi_handle, $active); | |
} while ($mrc == CURLM_CALL_MULTI_PERFORM); | |
} | |
} | |
//依次提取连接内容 | |
foreach ($urls as $i => $url) | |
{ | |
$contents[$i]=curl_multi_getcontent($conn[$i]); | |
curl_multi_remove_handle($multi_handle,$conn[$i]); | |
curl_close($conn[$i]); | |
} | |
curl_multi_close($multi_handle); | |
return $contents; | |
} | |
/** | |
* 一次只读一个连接 | |
*/ | |
function curl($urls){ | |
foreach($urls as $i => $url){ | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$timeout = 3; | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
$contents[$i] = curl_exec($ch); | |
} | |
return $contents; | |
} | |
// 测试 | |
$runtime = new Runtime; | |
$urls = array( | |
'baidu.com', | |
'google.com', | |
'bing.com', | |
'youdao.com', | |
'yahoo.com', | |
); | |
echo '<br><br>single connection curl'; | |
$runtime->start(); | |
$resArray = curl($urls); | |
$runtime->stop(); | |
$runtime->printResult(); | |
echo '<br><br>multiple sub connections curl'; | |
$runtime->start(); | |
$resArray = multiCurl($urls); | |
$runtime->stop(); | |
$runtime->printResult(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment