php: get_headers 函数结果不同

发布于 2024-08-07 22:15:23 字数 1476 浏览 1 评论 0原文

我在 PHP 中使用 get_headers 函数从网站请求标头 在本地服务器返回数组 在我的网站中使用时放置不返回数组

返回的示例

服务器中的本地服务器中

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
        )
)

在真实

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 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....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮了一弯 2024-08-14 22:15:23

PHP 在本地服务器和真实服务器上处理重定向的方式似乎有所不同。我认为您也会在本地获取数组,但由于某种原因,本地 get_headers() 似乎不遵循重定向。

两个环境中的 PHP 版本是否相同?

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?

喜爱纠缠 2024-08-14 22:15:23

这似乎没有理由。您应该将第二个参数设置为非零值才能获取数组1

get_headers($url, 1);

如果你这样做,它在任何地方都应该运行相同的,除非 PHP 本身或有问题的服务器存在错误(对于偶尔的用户来说,这两种情况都是罕见的)。

请注意,get_headers 遵循(多个)重定向,并将每个重定向的标头存储为数组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"
  }
...

重定向的特定标头值连续存储,因此看起来像 Content-Type[0]可以与任何 Location 相关,这使得数组格式无法正确获取每个重定向的标头。行数组格式也好不了多少,因为您需要解析标头。但使用数组格式,您可以检测最后一个位置等。

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

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 array2:

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 Locations, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文