编程语言
首页 > 编程语言> > PHP – `get_headers`为有效的URL返回“400 Bad Request”和“403 Forbidden”?

PHP – `get_headers`为有效的URL返回“400 Bad Request”和“403 Forbidden”?

作者:互联网

描述底部的工作解决方案!

我正在运行PHP 5.4,并试图获取URL列表的标题.

在大多数情况下,一切都运行良好,但有三个URL导致问题(可能更多,更广泛的测试).

'http://www.alealimay.com'
'http://www.thelovelist.net'
'http://www.bleedingcool.com'

这三个站点在浏览器中都可以正常工作,并生成以下标头响应:

(来自Safari)

successful headers

请注意,所有三个标头响应都是Code = 200

但是通过PHP检索标头,使用get_headers ……

stream_context_set_default(array('http' => array('method' => "HEAD")));
$headers = get_headers($url, 1);
stream_context_set_default(array('http' => array('method' => "GET")));

…返回以下内容:

url  ......  "http://www.alealimay.com"

headers
|    0  ............................  "HTTP/1.0 400 Bad Request"
|    content-length  ...............  "378"
|    X-Synthetic  ..................  "true"
|    expires  ......................  "Thu, 01 Jan 1970 00:00:00 UTC"
|    pragma  .......................  "no-cache"
|    cache-control  ................  "no-cache, must-revalidate"
|    content-type  .................  "text/html; charset=UTF-8"
|    connection  ...................  "close"
|    date  .........................  "Wed, 24 Aug 2016 01:26:21 UTC"
|    X-ContextId  ..................  "QIFB0I8V/xsTFMREg"
|    X-Via  ........................  "1.0 echo109"



url  ......  "http://www.thelovelist.net"

headers
|    0  ............................  "HTTP/1.0 400 Bad Request"
|    content-length  ...............  "378"
|    X-Synthetic  ..................  "true"
|    expires  ......................  "Thu, 01 Jan 1970 00:00:00 UTC"
|    pragma  .......................  "no-cache"
|    cache-control  ................  "no-cache, must-revalidate"
|    content-type  .................  "text/html; charset=UTF-8"
|    connection  ...................  "close"
|    date  .........................  "Wed, 24 Aug 2016 01:26:22 UTC"
|    X-ContextId  ..................  "aNKvf2RB/bIMjWyjW"
|    X-Via  ........................  "1.0 echo103"



url  ......  "http://www.bleedingcool.com"

headers
|    0  ............................  "HTTP/1.1 403 Forbidden"
|    Server  .......................  "Sucuri/Cloudproxy"
|    Date  .........................  "Wed, 24 Aug 2016 01:26:22 GMT"
|    Content-Type  .................  "text/html"
|    Content-Length  ...............  "5311"
|    Connection  ...................  "close"
|    Vary  .........................  "Accept-Encoding"
|    ETag  .........................  "\"57b7f28e-14bf\""
|    X-XSS-Protection  .............  "1; mode=block"
|    X-Frame-Options  ..............  "SAMEORIGIN"
|    X-Content-Type-Options  .......  "nosniff"
|    X-Sucuri-ID  ..................  "11005"

无论是否更改stream_context,都是这种情况

//stream_context_set_default(array('http' => array('method' => "HEAD")));
$headers = get_headers($url, 1);
//stream_context_set_default(array('http' => array('method' => "GET")));

产生相同的结果.

没有任何警告或错误被抛出(通常使用@get_headers抑制错误,但两种方式都没有区别).

我检查了我的php.ini,并将allow_url_fopen设置为On.

我将前往stream_get_meta_data,对CURL解决方案不感兴趣. stream_get_meta_data(及其伴随的fopen)将在与get_headers相同的位置失败,因此在这种情况下修复一个将解决.

通常,如果存在重定向,则输出如下所示:

url  ......  "http://www.startingURL.com/"

headers
|    0  ............................  "HTTP/1.1 301 Moved Permanently"
|    1  ............................  "HTTP/1.1 200 OK"
|    Date
|    |    "Wed, 24 Aug 2016 02:02:29 GMT"
|    |    "Wed, 24 Aug 2016 02:02:32 GMT"
|    
|    Server
|    |    "Apache"
|    |    "Apache"
|    
|    Location  .....................  "http://finishingURL.com/"
|    Connection
|    |    "close"
|    |    "close"
|    
|    Content-Type
|    |    "text/html; charset=UTF-8"
|    |    "text/html; charset=UTF-8"
|    
|    Link  .........................  "; rel=\"https://api.w.org/\", ; rel=shortlink"

为什么网站在浏览器中工作,但在使用get_headers时失败?

有各种SO帖子讨论相同的事情,但所有这些的解决方案都不属于这种情况:

POST requires Content-Length(我正在发送HEAD请求,不会返回任何内容)

URL contains UTF-8 data(这些网址中的唯一字符全部来自拉丁字母)

Cannot send a URL with spaces in it(这些网址都是无空间的,并且在各方面都很普通)

解!

(感谢Max在下面的答案中指出我在正确的轨道上.)

问题是因为没有预先定义的user_agent,没有在php.ini中设置,或在代码中声明它.

因此,我将user_agent更改为模仿浏览器,执行操作,然后将其还原为说明值(可能为空).

$OriginalUserAgent = ini_get('user_agent');
ini_set('user_agent', 'Mozilla/5.0');

$headers = @get_headers($url, 1);

ini_set('user_agent', $OriginalUserAgent);

用户代理更改发现here.

最佳答案:

之所以发生这种情况,是因为所有这三个站点都在检查请求和响应的UserAgent标头,如果无法匹配则会出现错误. get_headers函数不发送此标头.您可以尝试使用cURL和此代码段来获取网站内容:

$url = 'http://www.alealimay.com';
$c = curl_init($url);
curl_setopt($c, CURLOPT_USERAGENT, 'curl/7.48.0');
curl_exec($c);
var_dump(curl_getinfo($c));

UPD:
没有必要使用cURL来设置用户代理标头.它也可以用ini_set(‘user_agent’,’Mozilla / 5.0′)完成;然后get_headers函数将使用配置值.

标签:php,get-headers
来源: https://codeday.me/bug/20190516/1115036.html