php - cURL 真的很慢

标签 php performance curl timeout

有谁知道为什么 php5 下的 cURL 在 speedO'light 服务器上下载几 kb 的文件时甚至会在 45 秒超时时如此缓慢甚至失败?

这里的代码是按要求提供的(尽管我提高了超时时间以防止脚本在执行期间失败,并将用户代理从初始 Chrome 更改为 Mozilla/4.0):

$ch = curl_init('http://www.somesite.com/' . $key);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.somesite.com/somereferer/');
// curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/530.5 (KHTML, like Gecko) Chrome/2.0.172.39 Safari/530.5');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0');
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 600);

最佳答案

嗯,可能是一些事情,也许一些冗长的输出会有某种错误

curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true); // some output will go to stderr / error_log
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$response = curl_exec($ch);
$errStr = curl_error($ch);
$errNum = curl_errno($ch);
$head = curl_getinfo($ch, CURLINFO_HEADER_OUT);
$ci = curl_getinfo($ch);
print_r(array($head, $errStr, $errNum, $ci));

有时用户代理会改变网站的响应方式,可能需要做如下事情:

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101');

关于php - cURL 真的很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1937188/

相关文章:

java - XSLT 性能注意事项

json - 使用 jq 从 ElasticSearch 结果中的 _source 文档中提取数据

php - 使用 PHP (curl) 从 JSON (Google Maps API) 中提取数据

c++ - 将 cURL 库加载到 Qt5 后出现错误

php - 如果在 Yii 中选中复选框,则弹出一个窗口

javascript - 关于数据表源数据访问

php - 如何从 symfony2 中的现有表生成实体?

javascript - 从 3.x 升级到 PHP 5.5 后,它似乎试图解析我的 JavaScript 注释(嵌入 PHP 包含文件中的脚本标记中)

c# - 跨多个数据中心在地理上扩展 Azure 云服务

Xcode + Perforce : it frequently shows me the spinning wheel for no reason! 我能做什么?