The code works, but it takes up to 10 seconds to load the page. Then I add curl_getinfo(), and discovered the redirect time has used 90% of the total time….
<?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() shows:
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] =>
)
See [redirect_time] => 9.080637 ?
It takes up 90% of the total time.
How to improve it?
I say the most likely is the transfer fase: your ‘ http://www.somesite.com/xxx‘ just being terribly slow (which is hard to fix if it isn’t your site), 3 redirects taking > 3s each. But you could check how fast the dns lookup & connects are, seem ok though in that data.