php 获取301跳转后真实的url
现介绍利用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/?>
热文精选
更多资讯推荐
更多- Stable Diffusion 2.0 发布,加强成人内容过滤
- .NET 7 正式发布
- A3Mall 开源商城系统 v2.1 发布
- FydeOS v14 版本更新:优化输入法体验+重构安卓子系统+全新应用启动器
- ThinkPHP V6.0.8版本发布——多环境变量配置支持
- 毕昇 JDK 8u292、11.0.11 发布!
- KubeVela 1.0:开启可编程式应用平台的未来
- Eclipse 4.19 稳定版发布
- 阿里巴巴 Arthas 3.5.0 版本发布,支持反编译打印行号和统一鉴权
- Debian 11 Bullseye 即将进入冻结,Debian 13 代号 Trixie
- Rancher 2.5 发布,新增支持边缘集群的 GitOps
- FlashDB IoT 超轻量级嵌入式数据库