21,887
社区成员
发帖
与我相关
我的任务
分享
public function multi(){
$page = 128;//ajax的分页数
$max_size = 10;//并发数
//url数组
$url_arr = array();
//生成url数组
for($i = 1;$i <= $page; $i++){
$url = 'http://www.wdzj.com/front_select-plat?params=&sort=0&currPage=';
$url .= $i;
$url_arr[] = $url;
}
$mh = curl_multi_init();
//每次只放出$max_size大小的url用来初始化
$count = count($url_arr);
for($i=0;$i<$count;$i++){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_arr[$i]);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_multi_add_handle($mh, $ch);
$requestMap[] = $ch;
if(count($requestMap)<$max_size && ++$i != $count) continue;//保证数组中每次有$max_size个请求且请求的url还没有用完
$active = null;
//执行批处理句柄
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);
}
}
$json_arr = array();//存放ajax返回的接口数据
//获取资源并且关闭句柄
foreach($requestMap as $ch){
$json_arr[] = curl_multi_getcontent($ch);
// file_put_contents($i.'.txt', $json);
$error = curl_error($ch);
if($error != '') exit('发生了错误:'.$error);
curl_multi_remove_handle($mh, $ch);
}
unset($requestMap);
}
curl_multi_close($mh);
file_put_contents('1.txt', json_encode($json_arr));
}
<?php
ini_set('display_errors','on');
error_reporting(E_ALL);
?>
[Thu May 05 17:08:52.673028 2016] [core:warn] [pid 20268:tid 320] AH00098: pid file D:/phpStudy/Apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Thu May 05 17:08:52.811036 2016] [mpm_winnt:notice] [pid 20268:tid 320] AH00455: Apache/2.4.10 (Win32) OpenSSL/1.0.1i mod_fcgid/2.3.9 configured -- resuming normal operations
[Thu May 05 17:08:52.811036 2016] [mpm_winnt:notice] [pid 20268:tid 320] AH00456: Apache Lounge VC9 Server built: Jul 19 2014 13:20:51
[Thu May 05 17:08:52.811036 2016] [core:notice] [pid 20268:tid 320] AH00094: Command line: 'D:\\phpStudy\\Apache\\bin\\httpd.exe -d D:/phpStudy/Apache'
[Thu May 05 17:08:52.814036 2016] [mpm_winnt:notice] [pid 20268:tid 320] AH00418: Parent: Created child process 21132
[Thu May 05 17:08:54.476131 2016] [mpm_winnt:notice] [pid 21132:tid 336] AH00354: Child: Starting 150 worker threads.
[Thu May 05 17:15:20.696222 2016] [core:error] [pid 21132:tid 1724] [client 127.0.0.1:60080] End of script output before headers: index.php