curl执行速度非常慢。

b2571416 2012-09-18 10:06:14
正常写了一个简单的curl抓取程序,但是执行之后不管是访问本地还是远程url速度都超过10秒响应。这是为什么啊
注:程序里有一个10*10的for循环,需要执行100次。
...全文
1464 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2012-09-18
  • 打赏
  • 举报
回复
sleep 只是减缓结果的产生,你已经都嫌慢了,还要有意使他更慢吗?

抓取的速度如何,取决于网络和对方响应的速度(curl_getinfo的结果中就有详细的报告)
b2571416 2012-09-18
  • 打赏
  • 举报
回复
嗯,谢谢唠叨,请问正常curl在POST抓取中都会很慢么(5秒以上),还是1秒以下。
用sleep试验的时候总感觉在一开始的请求过程中会有一个缓慢的过程(类似阻塞,5秒左右),然后第一个提交返回之后就正常了。
xuzuning 2012-09-18
  • 打赏
  • 举报
回复
你在循环中执行 curl 100次,假定每次耗时 1 秒(不算多吧)那么就是 100 秒了

你可以 curl_multi 并发一下,那就快多了
b2571416 2012-09-18
  • 打赏
  • 举报
回复
一开始我考虑可能是一次连续请求造成的,后来加入休眠停顿也不行。
如下:

for($x=0;$x<=9;$x++){
$z[]=$x;
}

foreach($z as $x5){
foreach($z as $x6){
$final=$x5.$x6.'';
$url = "http://test.localhost.cn/postvalue.php";
$post_data = array (
"number" => $final,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$output = curl_exec($ch);
curl_close($ch);
//若某个结果包含某个信息 匹配则输出密码
if(count(explode('false',$output))==1){
echo "<span style='color: red;'> 输出密码$final 高亮</span></br>";
}
echo $output;

ob_flush();
flush();
sleep(0.1);
ob_end_flush();

}
}
b2571416 2012-09-18
  • 打赏
  • 举报
回复
代码如下:

for($x=0;$x<=9;$x++){
$z[]=$x;
}

foreach($z as $x5){
foreach($z as $x6){
$final=$x5.$x6.'';
$url = "http://test1.edgesoft.cn/postvalue.php";
$post_data = array (
"number" => $final,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$output = curl_exec($ch);
curl_close($ch);
//若某个结果包含某个信息 匹配则输出密码
if(count(explode('false',$output))==1){
echo "<span style='color: red;'> 输出密码$final 高亮</span></br>";
}
echo $output;



}
}
一起混吧 2012-09-18
  • 打赏
  • 举报
回复
对,可能就是那 10* 10 循环拖延了时间.
xuzuning 2012-09-18
  • 打赏
  • 举报
回复
需要看到代码

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧