$http_response_header — HTTP Response-Header
Das $http_response_header-Array ist vergleichbar mit der Funktion get_headers(). Bei der Verwendung eines HTTP-Wrappers enthält $http_response_header die HTTP Response-Header.
Beispiel #1 $http_response_header-Beispiel
<?php
file_get_contents("http://example.com");
var_dump($http_response_header);
?>
Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:
array(9) { [0]=> string(15) "HTTP/1.1 200 OK" [1]=> string(35) "Date: Sat, 12 Apr 2008 17:30:38 GMT" [2]=> string(29) "Server: Apache/2.2.3 (CentOS)" [3]=> string(44) "Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT" [4]=> string(27) "ETag: "280100-1b6-80bfd280"" [5]=> string(20) "Accept-Ranges: bytes" [6]=> string(19) "Content-Length: 438" [7]=> string(17) "Connection: close" [8]=> string(38) "Content-Type: text/html; charset=UTF-8" }
Here's a few pointers I've learned about using the $http_response_header variable:
1) The value changes with each request made.
2) When used in methods/functions, the current value must be passed to the method/function.
Using $http_response_header directly in the method/function without being assigned a value by a function/method parameter will result in the error message: Notice: Undefined variable: http_response_header
3) The array length and value locations in the array may change depending on the server being queried and the response received. I'm not sure if there are any 'absolute' value positions in the array.
4) $http_response_header ONLY gets populated using file_get_contents() when using a URL and NOT a local file. This is stated in the description when it mentions the HTTP_wrapper.
Example of using $http_response_header in a class.
This example will show how the output changes between the two requests.
<?php
class foo
{
private function header_code( $http_response_header )
{
// Display return header status
echo 'Status: ' . $http_response_header[0];
// Display easy-to-read valid PHP array code
echo '<pre>' . var_export( $http_response_header, true ) . '</pre>';
}
public function test()
{
// Make first request
$req1 = file_get_contents( 'http://www.google.com/' );
// Show header data
$this->header_code( $http_response_header );
// Make second request - ( Will have a 301 return header status )
$req2 = file_get_contents( 'http://google.com' );
// Show header data
$this->header_code( $http_response_header );
}
}
$test = new foo;
$test->test();
/* Output:
Status: HTTP/1.0 200 OK
array (
0 => 'HTTP/1.0 200 OK',
1 => 'Date: Mon, 07 Jun 2010 18:47:10 GMT',
2 => 'Expires: -1',
3 => 'Cache-Control: private, max-age=0',
4 => 'Content-Type: text/html; charset=ISO-8859-1',
5 => 'Set-Cookie: PREF=ID=410f407bde472198; expires=Wed, 06-Jun-2012 18:47:10 GMT; path=/; domain=.google.com',
6 => 'Server: gws',
7 => 'X-XSS-Protection: 1; mode=block',
8 => 'Alternate-Protocol: 443:npn-spdy/1',
)
Status: HTTP/1.0 301 Moved Permanently
array (
0 => 'HTTP/1.0 301 Moved Permanently',
1 => 'Location: http://www.google.com/',
2 => 'Content-Type: text/html; charset=UTF-8',
3 => 'Date: Mon, 07 Jun 2010 18:47:10 GMT',
4 => 'Expires: Wed, 07 Jul 2010 18:47:10 GMT',
5 => 'Cache-Control: public, max-age=2592000',
6 => 'Server: gws',
7 => 'Content-Length: 219',
8 => 'X-XSS-Protection: 1; mode=block',
9 => 'HTTP/1.0 200 OK',
10 => 'Date: Mon, 07 Jun 2010 18:47:10 GMT',
11 => 'Expires: -1',
12 => 'Cache-Control: private, max-age=0',
13 => 'Content-Type: text/html; charset=ISO-8859-1',
14 => 'Set-Cookie: PREF=ID=86ae94efdb5499c2; expires=Wed, 06-Jun-2012 18:47:10 GMT; path=/; domain=.google.com',
15 => 'Server: gws',
16 => 'X-XSS-Protection: 1; mode=block',
17 => 'Alternate-Protocol: 443:npn-spdy/1',
)
*/
?>