我有这个功能来检测页面的重定向最终URL
因此,如果我使用google.com调用此功能,它会检测到重定向并带回http://www.google.co.ve或http://www.google.co.ar,http://www.google.co.il等...
这告诉我这是正确检测到最终的URL ...现在当我尝试使用像t.co这样的缩短服务时,我使用此链接作为示例... t.co/0BWUquGyj3它将返回t。再次co / 0BWUquGyj3,而不是检测返回URL
function parse_url($url, $user_agent = 'desktop') {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, 45);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 45);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_TIMEOUT, 120);
curl_setopt($curl, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");
$curl_exec = curl_exec($curl);
$header = curl_getinfo($curl);
$final_url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
curl_close($curl);
return $final_url;
}
#http://t.co/0BWUquGyj3
$cosas = parse_html("t.co/0BWUquGyj3");
echo $cosas; //returns t.co/0BWUquGyj3 instead of the final redirect URL
答案 0 :(得分:0)
function unshorten_url($url) {
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_FOLLOWLOCATION => TRUE, // the magic sauce
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYHOST => FALSE, // suppress certain SSL errors
CURLOPT_SSL_VERIFYPEER => FALSE,
));
curl_exec($ch);
return curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
}
输出:
echo unshorten_url("t.co/0BWUquGyj3"); // https://twitter.com/zaynmalik/status/626878040472223744/photo/1