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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...