• 欢迎访问春风博客

获取URL跳转后的真实URL

运维笔记 xiaogang 8年前 (2016-02-10) 1488次浏览 0个评论

获取文件或跳转后的真实URL地址
第一种方法

function getrealurl($url){
$header = get_headers($url,1);
if (strpos($header[0],'301') || strpos($header[0],'302')) {
    if(is_array($header['Location'])) {
        return $header['Location'][count($header['Location'])-1];
    }else{
        return $header['Location'];
    }
}else {
    return $url;
}
}
//使用方法 
echo getrealurl($url);

第二种方法CURL

function curl_post_302($url,$data=null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 获取转向后的内容 
    $data = curl_exec($ch);
    $Headers = curl_getinfo($ch); 
    curl_close($ch);
    if($data != $Headers){ 
        return $Headers["url"]; 
        }else{
            return false;
        }
}
//使用方法
echo curl_post_302($url);

ChunBlog.Com , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:获取URL跳转后的真实URL
喜欢 (1)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址