In response to the gimme_headers() and get_headers() methods below:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
As described in 9.4 - "HEAD":
The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request.
So use HEAD instead because it SHOULD be much faster than GET...
For this behaviour it is much simpler to create a stream for the additional context param in all file functions:
<?php
header('Content-Type:text/plain');
$HTTP_HEAD = stream_context_create(array('http' => array('method' => 'HEAD')));
$fp = fopen('http://www.php.net', 'r', false, $HTTP_HEAD);
$headers = stream_get_meta_data($fp);
fclose($fp);
echo(var_export($headers));
?>
which should give something like this:
<?php
array (
'wrapper_data' =>
array (
0 => 'HTTP/1.1 200 OK',
1 => 'Date: Wed, 12 Aug 2009 13:00:32 GMT',
2 => 'Server: Apache/1.3.41 (Unix) PHP/5.2.9RC3-dev',
3 => 'X-Powered-By: PHP/5.2.9RC3-dev',
4 => 'Last-Modified: Wed, 12 Aug 2009 11:50:23 GMT',
5 => 'Content-language: en',
6 => 'Set-Cookie: expires=Wed, 19-Aug-2009 13:00:32 GMT; path=/; domain=.php.net',
7 => 'Connection: close',
8 => 'Content-Type: text/html;charset=utf-8',
),
'wrapper_type' => 'http',
'stream_type' => 'tcp_socket',
'mode' => 'r+',
'unread_bytes' => 0,
'seekable' => false,
'uri' => 'http://www.php.net',
'timed_out' => false,
'blocked' => true,
'eof' => false,
)
?>
headers_list
(PHP 5)
headers_list — Returns a list of response headers sent (or ready to send)
Description
array headers_list
( void
)
headers_list() will return a list of headers to be sent to the browser / client. To determine whether or not these headers have been sent yet, use headers_sent().
Return Values
Returns a numerically indexed array of headers.
Examples
Example #1 Examples using headers_list()
<?php
/* setcookie() will add a response header on its own */
setcookie('foo', 'bar');
/* Define a custom response header
This will be ignored by most clients */
header("X-Sample-Test: foo");
/* Specify plain text content in our response */
header('Content-type: text/plain');
/* What headers are going to be sent? */
var_dump(headers_list());
?>
The above example will output:
array(4) {
[0]=>
string(23) "X-Powered-By: PHP/5.1.3"
[1]=>
string(19) "Set-Cookie: foo=bar"
[2]=>
string(18) "X-Sample-Test: foo"
[3]=>
string(24) "Content-type: text/plain"
}
See Also
- headers_sent() - Checks if or where headers have been sent
- header() - Send a raw HTTP header
- setcookie() - Send a cookie
- apache_response_headers() - Fetch all HTTP response headers
headers_list
rayro at gmx dot de
12-Aug-2009 09:13
12-Aug-2009 09:13
slick[ at ]iqlogin[ dot ]net
08-Apr-2008 12:52
08-Apr-2008 12:52
To: leo at leoberkhout dot nl,
in PHP 5.0 get_headers() already exists so when i went through your code and organized it i had a small problem with that. Other than that when i changed the function name it worked fine. Of course i did tweak a few minor things to make it so. But this is what i've got.
<?php
function gimme_headers($host,$url) {
$headers = array();
$fp = fsockopen ($host,80,$errno,$errstr,45);
if($fp) {
fputs($fp,"GET $url HTTP/1.0\r\n\r\n");
while(!feof($fp)) {
$char = fgetc($fp);
if($char === "\n") {
if(ord($header) === 13) {
return($headers);
}
else {
array_push($headers,trim($header));
}
unset($header);
}
else {
$header = $header . $char;
}
}
fclose ($fp);
}
}
if(isset($_GET['uri'])) {
$uri = $_GET['uri'];
}
else {
$uri = 'php.net';
}
$array = gimme_headers($uri, "/");
print 'Cookie list for ' . $uri;
print '<ul>';
foreach ($array as $key => $value) {
print '<li>' . $_SERVER[$key] . ' = ' . $value . '</li>';
}
print '</ul>';
?>
leo at leoberkhout dot nl
24-Oct-2006 04:40
24-Oct-2006 04:40
If I change in de above code "php.net" for antoher URL, de code doesn't work properly.
How can it be made in a way like this:
http://webtools.live2support.com/header.php
I spent a hole evening, but I found only a way to make the output more readable:
<?php
function get_headers($host, $url)
{
$headers = array();
$fp = fsockopen ($host, 80, $errno, $errstr, 45);
if ($fp)
{
fputs ($fp, "GET $url HTTP/1.0\r\n\r\n");
while (!feof($fp))
{
$char = fgetc($fp);
if($char === "\n")
{
if (ord($header) === 13) { return($headers); }
else { array_push($headers, trim($header)); }
unset($header);
}
else { $header = $header.$char; }
}
fclose ($fp);
}
}
$Array = get_headers("php.net", "/");
// print_r(get_headers("php.net", "/"));
echo "<P>\n";
foreach ($Array as $Key => $Value)
{
echo "<LI>\$_SERVER[\"$Key\"]=$Value\n";
if ($Key == 6)
$Cookie = $Value;
} # End of foreach ($_SERVER as $Key=>$Value)
echo "<P>\n";
echo "Cookie = $Cookie<BR>\n";
?>
Here I do a check with, for example, key 6, to seperated them from the other values, just for example.
My problem is that the code does not accept www.mysite.com or http://www.mysite.com
robertbienert at gmx dot net
27-Sep-2006 09:59
27-Sep-2006 09:59
Also note that Lukes code could be simplified if using the HTTP HEAD method, because it returns only the HTTP headers, not the whole resource.
Luke at Bonanomi dot com
02-Sep-2006 09:56
02-Sep-2006 09:56
I arrived here looking for a way to collect headers sent by a remote server. No such luck; so:
<?php
function get_headers($host, $url)
{
$headers = array();
$fp = fsockopen ($host, 80, $errno, $errstr, 45);
if ($fp)
{
fputs ($fp, "GET $url HTTP/1.0\r\n\r\n");
while (!feof($fp))
{
$char = fgetc($fp);
if($char === "\n")
{
if (ord($header) === 13) { return($headers); }
else { array_push($headers, trim($header)); }
unset($header);
}
else { $header = $header.$char; }
}
fclose ($fp);
}
}
?>
print_r(get_headers("php.net", "/"));
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 02 Sep 2006 13:47:02 GMT
[2] => Server: Apache/1.3.37 (Unix) PHP/5.2.0-dev
[3] => X-Powered-By: PHP/5.2.0-dev
[4] => Last-Modified: Sat, 02 Sep 2006 13:21:09 GMT
[5] => Content-language: en
[6] => Set-Cookie: COUNTRY=USA%2C68.37.136.230; expires=Sat, 09-Sep-2006 13:47:02 GMT; path=/; domain=.php.net
[7] => Connection: close
[8] => Content-Type: text/html; charset=utf-8
)
