I want to send a request using cURL and retrieve the response header.
Using a browser the response header is as follow:
HTTP/1.0 302 Moved Temporarily
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Location: "Correct URL"
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Date: Tue, 30 Oct 2012 08:32:24 GMT
Server: Google Frontend
Content-Length: 0
But when I send the request using cURL the response header is as follow:
HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Location: "Wrong URL"
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Date: Tue, 30 Oct 2012 09:12:14 GMT
Server: Google Frontend
Content-Length: 0
I want to know what is causing the response to return different URLs. This is a small php sample out of many samples and things I tried with no avail.
<?php
$url = "url";
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.4) Gecko/20091030 Gentoo Firefox/3.5.4" );
list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $ch ), 2 );
curl_close( $ch );
$header_text = preg_split( '/[\r\n]+/', $header );
foreach ( $header_text as $headers ) {
echo $headers . "</br>";
}
?>
There is some difference between the requests sent through the browser and through curl (almost certainly in the HTTP headers) that causes the difference in the responses.
You should capture the request from the browser (perhaps using an HTTP proxy like Fiddler for convenience) and compare its headers to those from your curl request. One (or more) of the differences you will find is the reason for what you are seeing.