PHP get_headers() reports different headers than CURL

后端 未结 2 1554
谎友^
谎友^ 2021-01-05 01:40

How is it possible that get_headers() could possibly return a different result than getting them by CURL? Here is my code:

header(\"Content-type         


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 02:25

    Add a different User-Agent header before get_headers:

    stream_context_set_default(
        array(
            'http' => array(
                'method' => 'HEAD',
                'header' => "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1\r\n"
            )
        )
    );
    

    And, might as well specify HEAD since you only want headers. With this change you get the right headers.

    OUTPUT

    get_headers() headers:
    
    Array
    (
        [0] => HTTP/1.0 200 OK
        [1] => Server: Apache
        [2] => X-FoxNews-EdgeTTL: 2m
        [3] => Content-Type: text/html;charset=UTF-8
        [4] => Cache-Control: max-age=76
        [5] => Date: Fri, 31 Aug 2012 07:53:24 GMT
        [6] => Connection: close
    )
    
    
    CURL headers
    
    Array
    (
        [0] => HTTP/1.1 200 OK
        [1] => Server: Apache
        [2] => X-FoxNews-EdgeTTL: 2m
        [3] => Content-Type: text/html;charset=UTF-8
        [4] => Cache-Control: max-age=76
        [5] => Date: Fri, 31 Aug 2012 07:53:24 GMT
        [6] => Connection: keep-alive
        [7] => 
        [8] => 
    )
    

提交回复
热议问题