PHP获得最终的重定向url

时间:2015-09-30 12:19:45

标签: php redirect curl url-redirection

我想从此处获取最终的重定向网址:http://thatsthem.com/searching?ff=true&q0=Rob+Stott实际重定向到此:http://thatsthem.com/search/Rob-Stott/325712f7

我尝试了其他stackoverflow答案的答案,这些答案适用于其他网站但不适用于上述链接。请帮忙。

1 个答案:

答案 0 :(得分:1)

对于此特定网站,重定向是通过JavaScript window.location.replace()完成的,因此您需要查看响应正文:

$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_URL, "http://thatsthem.com/searching?ff=true&q0=Rob+Stott");
$html = curl_exec($c);
$redirection_url = preg_match("/window\.location\.replace\('(.*?)'\)/", $html, $m) ? $m[1] : null;
echo $redirection_url; // http://thatsthem.com/search/Rob-Stott/325712f7
相关问题