【问题标题】:curl slow connect_time卷曲慢连接时间
【发布时间】:2012-06-21 17:05:08
【问题描述】:

在我的 VPS 服务器上的 php 文件中考虑这段代码:

<?php $url = 'http://www.google.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3)     Gecko/20041001 Firefox/0.10.1" );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
$ci = curl_getinfo($ch);
curl_close($ch);
var_dump($ci); ?>

返回这个

array(22) { 
["url"]=> string(21) "http://www.google.com" 
["content_type"]=> string(24) "text/html; charset=UTF-8" 
["http_code"]=> int(200) 
["header_size"]=> int(2055) 
["request_size"]=> int(147) 
["filetime"]=> int(-1) 
["ssl_verify_result"]=> int(0) 
["redirect_count"]=> int(0) 
["total_time"]=> float(50.095466) 
["namelookup_time"]=> float(0.001114) 
["connect_time"]=> float(50.019724) 
["pretransfer_time"]=> float(50.019825) 
["size_upload"]=> float(0) 
["size_download"]=> float(23156) 
["speed_download"]=> float(462) 
["speed_upload"]=> float(0) 
["download_content_length"]=> float(-1) 
["upload_content_length"]=> float(0) 
["starttransfer_time"]=> float(50.070702) 
["redirect_time"]=> float(0) 
["certinfo"]=> array(0) { } 
["redirect_url"]=> string(0) 
"" }

在一天中多次测试后,“connect_time”始终保持在 50 秒标记处。如果不是在 1 秒及以下,我相信它应该快 10 倍。

我不了解服务器配置,但有人告诉我,我的服务器的 CPU 或 RAM 可能有问题。我使用顶部命令行显示以下对我来说似乎很好:

任务:总共 80 个,1 个正在运行,79 个正在休眠,0 个停止,0 个僵尸

Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st

内存:总计 2097152k,已使用 1273128k,免费 824024k,0k 缓冲区

交换:总共 0k,使用 0k,免费 0k,0k 缓存

我想知道这个问题的根源是什么?

编辑:附加信息

ping www.google.com 结果。似乎它可以一直持续下去,所以我在第 195 行之后停止了命令

PING www.l.google.com (74.125.224.178) 56(84) 字节数据。

来自 lax02s01-in-f18.1e100.net (74.125.224.178) 的 64 个字节:icmp_seq=1 ttl=56 时间=12.0 毫秒

来自 lax02s01-in-f18.1e100.net (74.125.224.178) 的 64 个字节:icmp_seq=2 ttl=56 时间=12.1 毫秒

来自 lax02s01-in-f18.1e100.net (74.125.224.178) 的 64 个字节:icmp_seq=3 ttl=56 时间=11.9 毫秒

...

来自 lax02s01-in-f18.1e100.net (74.125.224.178) 的 64 个字节:icmp_seq=194 ttl=56 时间=11.9 毫秒

来自 lax02s01-in-f18.1e100.net (74.125.224.178) 的 64 个字节:icmp_seq=195 ttl=56 时间=11.9 毫秒

--- www.l.google.com ping 统计 --- 发送 195 个数据包,接收 194 个数据包,0% 数据包丢失,时间 194711 毫秒

traceroute wwww.google.com 结果

到 www.google.com (74.125.224.180) 的跟踪路由,最多 30 跳,60 字节 数据包

1 ip---*-.ip.secureserver.net (.*..) 0.585 毫秒 0.642 毫秒 0.778 毫秒

2 be10.trmd0215-01.ars.mgmt.phx3.gdg (208.109.112.126) 0.599 毫秒 0.777 毫秒 0.893 毫秒

3 ip-97-74-253-122.ip.secureserver.net (97.74.253.122) 11.840 毫秒 12.119 毫秒 12.275 毫秒

4 ip-97-74-253-122.ip.secureserver.net (97.74.253.122) 12.389 毫秒 12.482 毫秒 12.600 毫秒

5 PR01.LAX03.google.com (206.223.123.21) 11.739 毫秒 11.709 毫秒 11.707 毫秒

6 209.85.248.185 (209.85.248.185) 11.986 毫秒 11.797 毫秒 11.781 毫秒

7 72.14.236.11 (72.14.236.11) 12.606 毫秒 12.363 毫秒 12.328 毫秒

8 lax02s01-in-f20.1e100.net (74.125.224.180) 11.774 毫秒 11.864 毫秒 11.842 毫秒

【问题讨论】:

  • 你有什么类型的网络连接,VPS主机上的资源是什么样的?对我来说,在多个平台上运行上述内容可以在 1-3 秒内完成。这是您设置的本地内容。您可以 ping/traceroute 到 google 并更新您的问题吗?这应该是服务器故障。
  • 谢谢 Sixeightzero,我已将这些信息添加到我的问题中
  • 将我的解析器配置 (resolv.config) 更改为 Google 的名称服务器 ip。没有帮助
  • > curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );这可以解决问题 首先,我将问题范围缩小到获取 url google.com 的 cURL 作业。其他 URL 很好,cURL 表现良好,即使使用 G 的 ip 地址作为 url。谷歌似乎启用了 IPv6,但流量没有正确/根本没有路由,并且可能 cURL 超时。非常感谢所有提供帮助的人。
  • 不错的收获!这是一个很好的问题和解决方案。

标签: php curl


【解决方案1】:

试试:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); 

以上将阻止 cURL 先尝试 IPv6。

【讨论】:

  • 如 OP cmets 中所述,这可以解决问题。 google.com 似乎启用了 IPV6,但流量路由不正确。 cURL 似乎超时了(这可以解释 50 秒的恒定 connect_time)并最终依靠 IPV4 进行连接。感谢所有帮助过的人
  • 我爱你 StackOverflow。这不仅对 Google.com 有帮助,而且对我的 API 调用也有帮助。我注意到我的远程服务器默认使用 IPv4,这就是为什么它比我的本地主机快得多。
  • 我们刚刚切换了服务器,新服务器出现超时错误。我设法找到问题的根源在于 curl 连接时间。我试图解决一整天的缓慢连接时间问题。你只是让我免于花费更多时间来解决这个可怕的问题。谢谢。
【解决方案2】:

我刚刚运行了您提供的相同配置并得到以下结果:

array
  'url' => string 'http://www.google.com' (length=21)
  'content_type' => string 'text/html; charset=UTF-8' (length=24)
  'http_code' => int 200
  'header_size' => int 2079
  'request_size' => int 151
  'filetime' => int -1
  'ssl_verify_result' => int 0
  'redirect_count' => int 0
  'total_time' => float 0.281
  'namelookup_time' => float 0
  'connect_time' => float 0
  'pretransfer_time' => float 0
  'size_upload' => float 0
  'size_download' => float 23168
  'speed_download' => float 82448
  'speed_upload' => float 0
  'download_content_length' => float -1
  'upload_content_length' => float 0
  'starttransfer_time' => float 0.187
  'redirect_time' => float 0
  'certinfo' => 
  array
    empty
  'redirect_url' => string '' (length=0)

我的猜测是您的服务器连接本身可能存在带宽问题和/或被另一个进程以某种方式征税。

【讨论】:

  • 非常感谢。是的,这是我认为这个 cURL 工作正常的时间值。您是否发现我在上面发布的 top 命令的结果有什么问题?
猜你喜欢
  • 2015-02-23
  • 2012-02-28
  • 2020-12-01
  • 2012-03-07
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 2019-08-16
相关资源
最近更新 更多