Show curl redirection but don't follow
I have the following code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.site.com/check.php?id=1");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
$curlData = curl_exec($curl);
curl_close($curl);
echo $curlData;
the script on the remote site will perform a certain check, and according to the check results it redirect to a small 15x15 gif image.
At the moment I have CURLOPT_FOLLOWLOCATION, 1
which means it will follow the redirectio开发者_如何学编程n to the gif and when I echo $curlData
I get the binary code of the image which is not what I want.
Is it possible to have curl display where the script tries to redirect me without actually following the redirect? So I can tell to which gif image it redirect me to instead of echoing the gif content?
Thanks,
Easily! Don't set CURLOPT_FOLLOWLOCATION
, and then read the Location
header from the response.
Edit: So, a bit more detail. The headers will be the lines of the response just after the status line, separated with \r\n
. You'll need to break up these lines, and look for the line prefixed with Location:
. This is a string parsing exercise - nothing terribly exciting or tricky. You can use curl_getinfo
with the CURLINFO_HEADER_SIZE
flag to discover the total length of the header portion of the response.
精彩评论