class curlRq{ / 获取url 302后的真实url / public static function getUrl302RealUrl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); // curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 20); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 下面两行为不验证证书和 HOST,建议在此前判断 URL 是否是 HTTPS curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // $ret 返回跳转信息 $ret = curl_exec($ch); // $info 以 array 形式返回跳转信息 $info = curl_getinfo($ch); // 跳转后的 URL 信息 $retURL = $info['url']; // 记得关闭curl curl_close($ch); return $retURL; } }

案例

php获取refererphp 应用curl获取短链的落地页url SQL

$url = 'https://v.douyin.com/iFUtsA1s/';$url = CurlReq::getUrl302RealUrl($url);var_dump($url);//输出 string(76) "https://www.douyin.com/video/7295947374797557026?previous_page=app_code_link"