显示curl重定向但不遵循
我有以下代码:
$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图像。
目前我有 CURLOPT_FOLLOWLOCATION, 1
这意味着它将跟随重定向到 gif,当我回显 $curlData
时,我得到图像的二进制代码,这不是什么我想。
是否可以在脚本尝试重定向我而不实际遵循重定向的地方显示curl?这样我就可以知道它会将我重定向到哪个 gif 图像,而不是回显 gif 内容?
谢谢,
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 redirection 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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
容易地!不要设置
CURLOPT_FOLLOWLOCATION
,然后从响应中读取Location
标头。编辑:所以,更详细一点。标头将是状态行之后的响应行,以
\r\n
分隔。您需要分解这些行,并查找前缀为Location:
的行。这是一个字符串解析练习——没什么特别令人兴奋或棘手的。您可以将curl_getinfo
与CURLINFO_HEADER_SIZE
标志结合使用来发现响应标头部分的总长度。Easily! Don't set
CURLOPT_FOLLOWLOCATION
, and then read theLocation
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 withLocation:
. This is a string parsing exercise - nothing terribly exciting or tricky. You can usecurl_getinfo
with theCURLINFO_HEADER_SIZE
flag to discover the total length of the header portion of the response.