繁体   English   中英

如何使用PHP或cURL获得真正的最终重定向URL?

[英]How to get the real final redirected URL using PHP or cURL?

我知道CURLINFO_EFFECTIVE_URLcurl_getinfo()函数可以得到最终的重定向URL。

但是,当我使用cURL尝试使用“ http://google.com ”或“ https://en.wikipedia.org/wiki/Redirection ”之类的URL并尝试获取CURLINFO_EFFECTIVE_URL ,它仍返回原始URL(“ http ://google.com ”和“ https://en.wikipedia.org/wiki/Redirection ”),而不是真正的重定向URL(“ https://www.google.com ”或“ https:// en。 wikipedia.org/wiki/Redirect ')。 (它们的每个HTTP状态代码分别是307和304)

在浏览器中,如果我输入“ http://google.com ”和“ https://en.wikipedia.org/wiki/Redirection ”,它将自动重定向到“ https://www.google.com ”并' https://en.wikipedia.org/wiki/Redirect '。

为什么CURLINFO_EFFECTIVE_URL在这里不起作用? 以及如何获得最终的重定向URL?


编辑

这是我的cURL PHP代码:

<?php
$url = "https://en.wikipedia.org/wiki/Redirection";
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
$redirected_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);

echo $redirected_url;    //It should show the redirected URL.
?>

我使用5.4.3作为我的PHP版本。 有什么事吗

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM