该代码有效,但加载页面最多需要 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%。
如何改进它?