[英]How can I get a 302 redirection URL's Location header in PHP?
I am trying to find a universal way to expand most if not all of the shortened URLs out there. 我试图找到一种通用的方法来扩展大多数(如果不是全部)缩短的URL。 I know short URLs such as bit.ly, TinyURL, goo.gl, etc use the 302 redirection method to redirect you to another site.
我知道bit.ly,TinyURL,goo.gl等短网址使用302重定向方法将您重定向到另一个网站。 How can I make a HEAD request to the shortened URL in php and get the "Location" part of the header?
如何在php中对缩短的URL发出HEAD请求并获取标题的“Location”部分?
Please help me with this. 请帮我解决一下这个。
Thanks 谢谢
Forget it everyone. 忘了每个人。 :) With some internet searching, I found this:
:)通过一些互联网搜索,我发现了这个:
expanding short url to original url using PHP and CURL — Hasin Hayder 使用PHP和CURL将短网址扩展到原始网址 - Hasin Hayder
It shows me exactly how to do this. 它告诉我如何做到这一点。
You need to use CURL. 你需要使用CURL。 You can set a callback function that fires to read headers.
您可以设置一个回调函数来触发读取标题。
//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.