curl slow connect_time [英] curl slow connect_time

查看:181
本文介绍了curl slow connect_time的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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秒标记处。我相信它应该是10倍快,如果不是在1秒标记和以下。



我不知道与服务器配置,但我被告知,我的服务器的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% si,
0.0%st



Mem:2097152k total,1273128k used,824024k free,0k
buffers



Swap:0k total,0k used,0k free,0k
cached


可能是此问题的根源?



编辑:其他信息



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 time = 12.0 ms



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



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



...



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



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



--- www.l.google.com ping统计---发送的195个数据包, 0%包丢失,时间194711ms


traceroute wwww.google.com结果



< blockquote>

traceroute到www.google.com(74.125.224.180),30跳最多,60字节
数据包



1 ip - - - * - .ip.secureserver.net(。*。)0.585 ms
0.642 ms 0.778 ms

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



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



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



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



6 209.85.248.185(209.85.248.185)11.986 ms 11.797 ms 11.781 ms



7 72.14.236.11(72.14.236.11)12.606 ms 12.363 ms 12.328 ms



8 lax02s01- inf20.1e100.net(74.125.224.180)11.774 ms 11.864 ms
11.842 ms



解决方案

试用:

  curl_setopt($ ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4); 

上述操作将阻止cURL首先尝试IPv6。


Consider this code in a php file on my VPS server:

<?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); ?>

returns this

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) 
"" }

After testing at several times of day, the "connect_time" is consistently at the 50 seconds mark. I believe it should be 10X faster if not at the 1 second mark and below.

I'm not knowledgeable with server config but I was told that my server's CPU or RAM could be at fault. I used the top command line to display the following which seems fine to me:

Tasks: 80 total, 1 running, 79 sleeping, 0 stopped, 0 zombie

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

Mem: 2097152k total, 1273128k used, 824024k free, 0k buffers

Swap: 0k total, 0k used, 0k free, 0k cached

I am wondering what could be the source of this issue ?

EDIT: ADDITIONAL INFORMATION

ping www.google.com result. It seemed like it could go on foreover so I stopped the command after the 195th line

PING www.l.google.com (74.125.224.178) 56(84) bytes of data.

64 bytes from lax02s01-in-f18.1e100.net (74.125.224.178): icmp_seq=1 ttl=56 time=12.0 ms

64 bytes from lax02s01-in-f18.1e100.net (74.125.224.178): icmp_seq=2 ttl=56 time=12.1 ms

64 bytes from lax02s01-in-f18.1e100.net (74.125.224.178): icmp_seq=3 ttl=56 time=11.9 ms

...

64 bytes from lax02s01-in-f18.1e100.net (74.125.224.178): icmp_seq=194 ttl=56 time=11.9 ms

64 bytes from lax02s01-in-f18.1e100.net (74.125.224.178): icmp_seq=195 ttl=56 time=11.9 ms

--- www.l.google.com ping statistics --- 195 packets transmitted, 194 received, 0% packet loss, time 194711ms

traceroute wwww.google.com result

traceroute to www.google.com (74.125.224.180), 30 hops max, 60 byte packets

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

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

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

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

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

6 209.85.248.185 (209.85.248.185) 11.986 ms 11.797 ms 11.781 ms

7 72.14.236.11 (72.14.236.11) 12.606 ms 12.363 ms 12.328 ms

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

解决方案

Try:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); 

The above will prevent cURL from trying IPv6 first.

这篇关于curl slow connect_time的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆