美好的一天!
请求页面时,cURL的行为非常慢。我知道它不是请求的页面,因为页面会立即在浏览器中返回。
我注意到的两件事
我有以下代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, false);
$output = curl_exec($ch);
curl_close($ch);
回应curl_getinfo()给我以下
[url] => http://127.0.0.1:80/wpengine/?json=t
[content_type] => text/html; charset=iso-8859-1
[http_code] => 302
[header_size] => 215
[request_size] => 64
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 17.238
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 221
[speed_download] => 12
[speed_upload] => 0
[download_content_length] => 221
[upload_content_length] => 0
[starttransfer_time] => 17.238
[redirect_time] => 0
[certinfo] => Array
(
)
[primary_ip] => 127.0.0.1
[primary_port] => 80
[local_ip] => 127.0.0.1
[local_port] => 51875
[redirect_url] =>
有谁可以给我一些关于如何弄清楚发生了什么的指示?
以下是Apache访问日志中的几行
127.0.0.1 - - [06/Dec/2013:12:01:22 -0500] "GET /wpengine/?json=t HTTP/1.1" 302 221
127.0.0.1 - - [06/Dec/2013:12:01:12 -0500] "GET /community HTTP/1.1" 200 6266
127.0.0.1 - - [06/Dec/2013:12:01:22 -0500] "GET /public/js/jquery.js?b=10 HTTP/1.1" 304 -
答案 0 :(得分:4)
这主要是因为在处理大型POST数据时,Expect: 100-continue
标头CURL发送到服务器,而服务器刚刚发生不支持此功能。您可以通过为-vv
命令行添加curl
来确认并查看输出。
要解决此问题,您可以
Expect: 100-continue
标头或-H "Expect:"
选项发送它(在php中,你可以参考这个答案:How can I stop cURL from using 100 Continue?)。关于Expect: 100-continue
的链接:
https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3 https://support.urbanairship.com/entries/59909909--Expect-100-Continue-Issues-and-Risks
答案 1 :(得分:3)
我在点击Google Analytics的MP API时发现了一个类似的问题......开始转移时间始终为1.0秒加上一小部分,我推断某处有1秒的延迟,我猜是在cURL中。它不是在我的最后使用CPU,我不知道谷歌是否正在推迟......来自curl_getinfo()的输出......
Array ( [url] => http://www.google-analytics.com/collect [content_type] => image/gif [http_code] => 200 [header_size] => 388 [request_size] => 201 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 1.021528 [namelookup_time] => 8.7E-5 [connect_time] => 8.8E-5 [pretransfer_time] => 0.000324 [size_upload] => 1736 [size_download] => 35 [speed_download] => 34 [speed_upload] => 1699 [download_content_length] => 35 [upload_content_length] => 1736 [starttransfer_time] => 1.002496 [redirect_time] => 0 [certinfo] => Array ( ) [redirect_url] => )
答案 2 :(得分:0)
该主题中@brandonscript的评论之一帮助我解决了问题。
他的答案:
很好奇,也许CURL是否先尝试IPv6并无法解决?
尝试添加 curl_setopt($ ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
我的问题正好相反,我必须设置以下选项:
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6 );
谢谢你,很棒的人。
答案 3 :(得分:-1)
我通过设置
解决了这个问题curl_setopt($ch, CURLOPT_POST, false);