A5下载文章资讯

分类分类

php 获取301跳转后真实的url

2015-06-09 15:40作者:sa

现介绍利用get_headers() 函数获取http头

php 自带的get_headers()取得服务器响应一个 HTTP 请求所发送的所有标头。 获取301状态肯定没问题。

301定向的例子:

google.com 会301跳转至 www.google.com

再www.google.com 会302跳转至 www.google.com.hk

我写了个php函数 其php函数作用:

输入 google.com 得到 www.google.com.hk

输入 www.google.com 得到 www.google.com.hk

输入 www.google.com.hk 得到 www.google.com.hk

<?php/* @param str $url 查询 $return str 定向后的url的真实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; }}$url = 'http://google.com';$url = getrealurl($url);echo '真实的url为:'.$url;//真实的url为:http://www.google.com.hk/?>

 

 

展开全部

相关

说两句网友评论
    我要跟贴
    取消