简体   繁体   中英

php: get_headers Function Results Are Different

i using get_headers Function in PHP to request headers from website in local server return arrays put when use in my website Does not return arrays

examples for returns

in local server

Array
(
    [0] => HTTP/1.1 301 Moved
    [Server] => Array
        (
            [0] => nginx/0.7.42
            [1] => Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4
            [2] => Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4
            [3] => Microsoft-IIS/7.0
        )
    [Content-Type] => Array
        (
            [0] => text/html; charset=utf-8
            [1] => text/html; charset=iso-8859-1
            [2] => text/html
            [3] => text/html; charset=utf-8
        )
    [Location] => Array
        (
            [0] => http//3.ly/aXP
            [1] => http//3.ly/aXP/
            [2] => http//stackoverflow.com
        )
    [MIME-Version] => 1.0
    [Content-Length] => Array
        (
            [0] => 277
            [1] => 376
            [2] => 0
            [3] => 122213
        )
)

in real server

Array
(
    [0] => HTTP/1.1 301 Moved
    [Server] => nginx/0.7.42
    [Date] => Sat, 10 Oct 2009 03:15:32 GMT
    [Content-Type] => text/html; charset=utf-8
    [Connection] => keep-alive
    [Location] => http//3.ly/aXP
    [MIME-Version] => 1.0
    [Content-Length] => 277
)

i wont to return arrays

thanks....

There seems to be a difference in how PHP handles redirects on your local server and on the real server. I think you would get arrays locally too, but for some reason get_headers() locally doesn't seem to follow redirects.

Is the PHP version same in both environments?

There doesn't seem a reason for this. You should set the second parameter to a non-zero value in order to get an array 1 :

get_headers($url, 1);

If you do that, it should run the same anywhere, unless there is a bug in PHP itself or in the problematic server (both are rare cases for the occasional user).

Note that get_headers follows (multiple) redirects and stores the headers of each redirect as an array 2 :

array(11) {
  [0]=>
  string(30) "HTTP/1.0 301 Moved Permanently"
  ["Location"]=>  string(22) "http://www.google.com/"
  ["Content-Type"]=>  array(2) {
    [0]=>    string(24) "text/html; charset=UTF-8"
    [1]=>    string(29) "text/html; charset=ISO-8859-1"
  }
...

The particular header values for redirects are stored successively, so it looks like Content-Type[0] can be related to any of the Location s, which makes the array format unusable to get the headers of each of the redirects correctly. Line array format is not much better, since you will need to parse the headers. But with the array format you can detect the last location etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM