A5下载站:努力做内容最丰富最安全的下载站! 网站地图最新更新下载排行专题软件发布

热门软件

地铁跑酷

冒险迷岛

全民迷宫

连连消大作战

小河狸创客

阿里健康医鹿

支付宝app

番薯小说

MOMO陌陌

虾米音乐app

位置导航:A5下载 > 源码技巧 > 父类数据

php 获取301跳转后真实的url

时间:2015-06-09 15:40来源:未知作者:sa浏览:135
现介绍利用get_headers() 函数获取http头 php 自带的get_headers()取得服务器响应一个 HTTP 请求所发送的所有标头。 获取301状态肯定没问题。 301定向的例子: google.com 会301跳转至 www.google.com 再www.google.com 会302跳转至 www.google.com.hk 我写……

现介绍利用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/?>