编程语言
首页 > 编程语言> > PHP遵循URL重定向

PHP遵循URL重定向

作者:互联网

如何在PHP中获取Airbnb短链接URL的最终目标网址? (例如https://abnb.me/Vt3MA7vVyM)

使用this tool我可以看到链接被重定向三次
enter image description here

解决方法:

file_get_contents()遵循HTTP响应中定义的重定向.但是,使用window.top.location在JavaScript中有一个重定向.所以,你可以使用strpos()和一个简单的preg_match()来解析它:

$url = 'https://abnb.me/Vt3MA7vVyM';
$ret = file_get_contents($url);
$pos = strpos($ret, 'window.top.location');
if ($pos !== false) {
    $str = substr($ret, $pos);
    $str = preg_match('~validate\("([^"]*)~', $str, $matches);
    echo html_entity_decode($matches[1]);
}

输出:

 https://airbnb.com/rooms/2110908?=&s=41&ref_device_id=43fb193006d0cb8848f689aec67ba15ae5c48471&user_id=10758532&_branch_match_id=519823851281523702

标签:php,redirect,url-redirection,airbnb
来源: https://codeday.me/bug/20190622/1262523.html