[英]How can I get a 302 redirection URL's Location header in PHP?
我试图找到一种通用的方法来扩展大多数(如果不是全部)缩短的URL。 我知道bit.ly,TinyURL,goo.gl等短网址使用302重定向方法将您重定向到另一个网站。 如何在php中对缩短的URL发出HEAD请求并获取标题的“Location”部分?
请帮我解决一下这个。
谢谢
你需要使用CURL。 您可以设置一个回调函数来触发读取标题。
//register a callback function which will process the headers
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'readHeader');
function readHeader($ch, $header)
{
global $location;
// we have to follow 302s automatically or cookies get lost.
if (eregi("Location:",$header) )
{
$location= substr($header,strlen("Location: "));
}
return strlen($header);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.