显示curl重定向但不遵循

发布于 2024-12-02 01:44:33 字数 704 浏览 0 评论 0原文

我有以下代码:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下凄凉 2024-12-09 01:44:33

容易地!不要设置 CURLOPT_FOLLOWLOCATION,然后从响应中读取 Location 标头。

编辑:所以,更详细一点。标头将是状态行之后的响应行,以 \r\n 分隔。您需要分解这些行,并查找前缀为 Location: 的行。这是一个字符串解析练习——没什么特别令人兴奋或棘手的。您可以将 curl_getinfoCURLINFO_HEADER_SIZE 标志结合使用来发现响应标头部分的总长度。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文