这个网站的链接,如何用php获得链接最终跳转的地址?
https://slickdeals.net/?adobeRef=94393328ca6e11e7a09daa569a0b14b40002&pno=332607&afsrc=1&trd=See%20Deal&sdtid=10743527&tid=10743527&sdtrk=featured-cat&pv=943d41c0ca6e11e7a09daa569a0b14b4&au=38b8859e863011e7b3c3bafcfc572079&attr_track=Meta%3ACheckout%3Anot%20eligible
https://slickdeals.net/?adobeRef=d2b27a7eca6e11e7a5b946e4c7109be00000&afsrc=1&trd=See%20Deal&sdtid=10813599&tid=10813599&sdtrk=catpagev2&pv=d2b654d2ca6e11e7a5b946e4c7109be0&au=38b8859e863011e7b3c3bafcfc572079&attr_track=Meta%3ACheckout%3Anot%20eligible
我的程序是这样的,采集到的却还是原网站地址。
<?php
$url=$_REQUEST['url'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//若给定url自动跳转到新的url,有了下面参数可自动获取新url内容:302跳转
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//设置cURL允许执行的最长秒数。
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0');
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$content = curl_exec($ch);
//获取请求返回码,请求成功返回200
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
//echo $code . "\n\n";
//获取一个cURL连接资源句柄的信息。
//$headers 中包含跳转的url路径
$headers = curl_getinfo($ch);
echo $headers[url];
// $content 为url请求内容
//echo "\n\n" . $content . "\n";
?>
或者参考这样:
$url=$_REQUEST['url'];
$realUrl=getRedirectLocation($url);
echo "</br>--->",$realUrl;
function getRedirectLocation($url){
$realUrl=$url;
echo $url,"</br>";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);curl_setopt($ch, CURLOPT_TIMEOUT, 3);//设置curl执行时间不超过3秒
//curl_setopt($ch, CURLOPT_NOBODY, 1);//这行不能要,如果添上,那么在遇到302重定向的时候就会得不到真正的请求url
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$content=curl_exec($ch);
//echo $content;
$rinfo=curl_getinfo($ch);
$matches=array();
if(preg_match('/Location:\s+?(.+?)\s+?/', $content,$matches)){
//echo $matches[1],"</br>";
unset($content);
$realUrl=getRedirectLocation($matches[1]);
}
if(isset($content)){
unset($content);
}
return $realUrl;
}
出来的也不对。 |