PHP:从重定向的URL获取链接地址 [英] PHP: Get link address from redirected URL
问题描述
如何在重定向网址后获取链接地址?
How can I get the link address after a URL has been redirected?
以此网址为例: http://www.boligsiden.dk/viderestilling/992cff55882a40f79e64b0a25e847a69
我如何制作一个PHP脚本回显最终的URL? ( http://www.eltoftnielsen.dk/default。 aspx?side = sagsvisning& AutoID = 125125& DID = 140 在这种情况下)
How can I make a PHP script echo the final URL? (http://www.eltoftnielsen.dk/default.aspx?side=sagsvisning&AutoID=125125&DID=140 in this case)
推荐答案
注意:以下解决方案不适合高流量情况。
Note: The following solution isn't ideal for high traffic situations.
$url = 'http://www.boligsiden.dk/viderestilling/992cff55882a40f79e64b0a25e847a69';
file_get_contents($url);
preg_match('/(Location:|URI:)(.*?)\n/', implode("\n", $http_response_header), $matches);
if (isset($matches[0]))
{
echo $matches[0];
}
以下是发生的事情:file_get_contents()重定向并下载目标网站但写入原始响应标头进入$ http_response_header。
Here's what happens: file_get_contents() redirects and downloads the target website but writes the original response header into $http_response_header.
preg_match尝试找到第一个Location:x匹配并返回它。
the preg_match tries to find the first "Location: x" match and returns it.
这篇关于PHP:从重定向的URL获取链接地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!