get_headers 没有遵循重定向 - 为什么不呢?

get_headers is not following redirects - why not?

我需要在遵循所有重定向后从站点检索 final 组 header。根据 PHP documentation, get_headers is supposed to follow redirects. However, I tested at phpfiddle.org and also on my own server using the url "http://microsoft.com" 中的评论。(PHP 5.x)

$r = get_headers('http://microsoft.com', 0);
    var_dump($r);

结果:

array(18) {
  [0]=>
  string(30) "HTTP/1.1 301 Moved Permanently"
  [1]=>
  string(38) "Content-Type: text/html; charset=UTF-8"
  [2]=>
  string(35) "Location: http://www.microsoft.com/"
  [3]=>
  string(25) "Server: Microsoft-IIS/8.5"
  [4]=>
  string(21) "X-Powered-By: ASP.NET"
  [5]=>
  string(35) "Date: Thu, 14 Jan 2016 19:55:58 GMT"
  [6]=>
  string(17) "Connection: close"
  [7]=>
  string(19) "Content-Length: 148"
  [8]=>
  string(15) "HTTP/1.0 200 OK"
  [9]=>
  string(14) "Server: Apache"
  [10]=>
  string(51) "ETag: "6082151bd56ea922e1357f5896a90d0a:1425454794""
  [11]=>
  string(44) "Last-Modified: Wed, 04 Mar 2015 07:39:54 GMT"
  [12]=>
  string(20) "Accept-Ranges: bytes"
  [13]=>
  string(20) "Content-Length: 1020"
  [14]=>
  string(23) "Content-Type: text/html"
  [15]=>
  string(35) "Date: Thu, 14 Jan 2016 19:55:59 GMT"
  [16]=>
  string(17) "Connection: close"
  [17]=>
  string(6) "X-N: S"
}

知道为什么这不起作用,以及如何让它起作用吗?

我知道它没有完全遵循重定向,因为如果您输入 URL:http://microsoft.com, you end up at http://www.microsoft.com/en-us。你可以看到 headers 不包含到这里的重定向。

我需要这样做的原因:我需要确定最后一页是否设置了 X-Frame-Options header。 http://microsoft.com and http://www.microsoft.com/ pages do not have this header, but the http://www.microsoft.com/en-us 页面 returns X-Frame-Options header,阻止页面在 iframe 中加载。

我需要通过检查此 header 来确定相关页面是否可以加载到 iframe 中,如果不能,我需要将用户重定向到目的地,而不是在 iframe 中显示该页面.

谢谢

get_header 是否 遵循重定向。当您仔细检查转储的 headers 时,您会注意到该数组包含 两个 组响应 headers:

这是第一个:

array(18) {
  [0]=>
  string(30) "HTTP/1.1 301 Moved Permanently"
  [1]=>
  string(38) "Content-Type: text/html; charset=UTF-8"
  [2]=>
  string(35) "Location: http://www.microsoft.com/"
  [3]=>
  string(25) "Server: Microsoft-IIS/8.5"
  [4]=>
  string(21) "X-Powered-By: ASP.NET"
  [5]=>
  string(35) "Date: Thu, 14 Jan 2016 19:55:58 GMT"
  [6]=>
  string(17) "Connection: close"
  [7]=>
  string(19) "Content-Length: 148"

第二个:

  [8]=>
  string(15) "HTTP/1.0 200 OK"
  [9]=>
  string(14) "Server: Apache"
  [10]=>
  string(51) "ETag: "6082151bd56ea922e1357f5896a90d0a:1425454794""
  [11]=>
  string(44) "Last-Modified: Wed, 04 Mar 2015 07:39:54 GMT"
  [12]=>
  string(20) "Accept-Ranges: bytes"
  [13]=>
  string(20) "Content-Length: 1020"
  [14]=>
  string(23) "Content-Type: text/html"
  [15]=>
  string(35) "Date: Thu, 14 Jan 2016 19:55:59 GMT"
  [16]=>
  string(17) "Connection: close"
  [17]=>
  string(6) "X-N: S"
}

来自 http://www.microsoft.com to http://www.microsoft.com/en-us is probably language specific and is dependent on the Accept-Language request header (when opening the same URL in my browser, I end up at http://www.microsoft.com/de-de 的最终重定向)。