php curl - curl_getinfo()中的长重定向时间

时间:2012-05-29 05:05:12

标签: php curl performance

代码有效,但加载页面最多需要10秒钟。然后我添加了curl_getinfo(),发现重定向时间占总时间的90%....

<?php
//
$url = "http://www.somesite.com/###";

$username = 'username';
$password = 'password';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

$out = curl_exec($ch);

print "error:" . curl_error($ch) . "<br />";
print "output:" . $out . "<br /><br />";

$info = curl_getinfo($ch);
print_r($info);

curl_close($ch);

?>

curl_getinfo()显示:

Array ( 
[url] => http://www.somesite.com/xxx
[content_type] => text/xml;charset=UTF-8 
[http_code] => 200 
[header_size] => 2760 
[request_size] => 5576 
[filetime] => -1 
[ssl_verify_result] => 0 
[redirect_count] => 3 
[total_time] => 10.751595 
[namelookup_time] => 2.0E-5 
[connect_time] => 0.001612 
[pretransfer_time] => 0.001613 
[size_upload] => 0 
[size_download] => 24506 
[speed_download] => 2279 
[speed_upload] => 0 
[download_content_length] => 24506 
[upload_content_length] => 0 
[starttransfer_time] => 0.62479 
[redirect_time] => 9.080637 <--------------
[certinfo] => Array ( ) 
[redirect_url] => 
) 

请参阅[redirect_time] => 9.080637

占总时间的90%。

如何改进?

1 个答案:

答案 0 :(得分:1)

  

CURLINFO_REDIRECT_TIME   将指针传递给double以接收所有重定向步骤所花费的总时间(以秒为单位),包括名称查找,连接,预传输和最终事务启动之前的传输。 CURLINFO_REDIRECT_TIME包含多个重定向的完整执行时间。 (在7.9.7中添加)

我说最有可能是转移费用:你的'http://www.somesite.com/xxx'只是非常慢(如果它不是你的网站就很难修复),3重定向&GT;每个3s。但是你可以检查dns查找速度和速度。虽然在那些数据中,连接似乎没问题。