php - PHP get_headers 有时将位置返回为数组
问题描述
我不明白为什么,有时
$headers = @get_headers($url , true);
将Location
字段作为数组而不是字符串返回......
所以如果我需要获取完整的网址,当它是一个字符串时,我会做
$unshorted_url = $headers['Location'];
但是当Location
作为数组返回时,我不知道我需要考虑什么索引......
有什么建议吗?更重要的是.....为什么Location
有时会以数组的形式返回?
当我尝试获取这样的 ebay 网址时,我看到了这个“问题”https://ebay.us/JOBBf0
解决方案
如果 URL 被重定向并且新目标也被重定向,我们会在数组中获取 Locations,并且还会在数字索引值中获取 HTTP 代码。
让我们想象一下这个重定向链:
/index.php?id=1
-> /index.php?id=2
->/index.php?id=3
位置将是:
[Location] => Array
(
[0] => /index.php?id=2
[1] => /index.php?id=3
)
这id=3
将是登录页面。
如果您想要第一个重定向(id=2
在示例中):
$headers = get_headers($url, true);
if (is_array($headers['Location'])) {
$unshorted_url = reset($headers['Location']);
} else {
$unshorted_url = $headers['Location'];
}
如果您想要登陆页面,您可以使用end()
而reset()
不是。
推荐阅读
- amazon-web-services - 与基础版本 SES 相比,SESv2 的 aws-sdk 是否缺少某些功能?
- jenkins - 如何通过 Jenkins 更新 ECS 容器/TASK?
- node.js - Stripe:从用户卡而不是我的账户余额进行转账
- azure-functions - 如何使用 Azure 媒体服务 v3 将现有 Blob 克隆到输入资产
- r - 为什么使用 R(数值积分)中的复合辛普森规则我的近似值太大?
- r - R:有没有相当于Stata的ibn。功能?
- spring - 如何在jpa实体休眠中添加条件注释
- python - Python 不移动文件
- python - 获取在特定时间修改的文件列表的最佳方法
- c# - 在尝试将 html.actionLink 添加到 Jquery 数据表时需要一些帮助