显示卷曲重定向但不跟随 [英] Show curl redirection but don't follow
问题描述
我有以下代码:
$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;
远程站点上的脚本将执行一定的检查,并根据检查结果重定向到一个小的15x15的gif图像。
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.
目前我有 CURLOPT_FOLLOWLOCATION,1
这意味着它将跟随重定向gif,当我回到 $ curlData
我得到的图像的二进制代码,这不是我想要的。
At the moment I have CURLOPT_FOLLOWLOCATION, 1
which means it will follow the redirection to the gif and when I echo $curlData
I get the binary code of the image which is not what I want.
是否可能有curl显示脚本尝试重定向我,而不实际跟踪重定向?所以我可以告诉它是哪个gif图像它重定向我,而不是回应gif内容?
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?
感谢,
推荐答案
轻松!不要设置 CURLOPT_FOLLOWLOCATION
,然后从响应中读取位置
标题。
Easily! Don't set CURLOPT_FOLLOWLOCATION
, and then read the Location
header from the response.
编辑:所以,更多的细节。标题将是状态行后面的响应行,用 \r\\\
分隔。您需要拆分这些行,并查找前缀为
位置:
的行。这是一个字符串解析练习 - 没有什么可怕的刺激或棘手。您可以使用 curl_getinfo
与 CURLINFO_HEADER_SIZE
标志来发现响应头部分的总长度。
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.
这篇关于显示卷曲重定向但不跟随的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!