我写了一个小脚本来验证URL是否存在。我正在使用get_headers检索标头。问题在于,使用某些URL,例如以下URL:https://forum.obviousidea.com,响应为403 Bad Behavior
,而如果我使用浏览器打开页面,则该URL有效。
示例输出:
$headers = get_headers(https://forum.obviousidea.com);
print_r($headers);
(
[0] => HTTP/1.1 403 Bad Behavior
[Server] => nginx/1.6.2
[Date] => Tue, 04 Jun 2019 21:56:27 GMT
[Content-Type] => text/html; charset=ISO-8859-1
[Content-Length] => 913
[Connection] => close
[Set-Cookie] => Array
(
[0] => bb_lastvisit=1559685385; expires=Wed, 03-Jun-2020 21:56:25 GMT; Max-Age=31536000; path=/; secure
[1] => bb_lastactivity=0; expires=Wed, 03-Jun-2020 21:56:25 GMT; Max-Age=31536000; path=/; secure
[2] => PHPSESSID=cqtkdcfpm0k2s8hl4cup6epa37; path=/
)
[Expires] => Thu, 19 Nov 1981 08:52:00 GMT
[Cache-Control] => private
[Pragma] => private
[Status] => 403 Bad Behavior
)
如何使用get_headers获得正确的状态代码?
请注意,使用答案中建议的用户代理,此网站现在可以正常工作。
但是例如,该URL仍然不起作用:https://filezilla-project.org/download.php?type=client
答案 0 :(得分:1)
您可能已通过php.ini或ini_set更改了UserAgent标头
检查它或像下面的示例一样设置UserAgent
data class SavedValue(private var _previousValue: String = "") {
var currentValue: String = ""
set(value) {
if (field != value) _previousValue = field
field = value
}
val previousValue: String
get() = _previousValue
}
我更喜欢使用波纹管卷曲功能:
ini_set('user_agent', '');
$headers = get_headers('https://forum.obviousidea.com');