GET_HEADERS(3) 1 GET_HEADERS(3)get_headers - Fetches all the headers sent by the server in response to a HTTP requestSYNOPSIS
array get_headers (string $url, [int $format])
DESCRIPTION get_headers(3) returns an array with the headers sent by the server in response to a HTTP request.
PARAMETERS
o $url
- The target URL.
o $format
- If the optional $format parameter is set to non-zero, get_headers(3) parses the response and sets the array's keys.
RETURN VALUES
Returns an indexed or associative array with the headers, or FALSE on failure.
CHANGELOG
+--------+---------------------------------------------------+
|Version | |
| | |
| | Description |
| | |
+--------+---------------------------------------------------+
| 5.1.3 | |
| | |
| | This function now uses the default stream con- |
| | text, which can be set/changed with the |
| | stream_context_set_default(3) function. |
| | |
+--------+---------------------------------------------------+
EXAMPLES
Example #1
get_headers(3) example
<?php
$url = 'http://www.example.com';
print_r(get_headers($url));
print_r(get_headers($url, 1));
?>
The above example will output something similar to:
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)
Array
(
[0] => HTTP/1.1 200 OK
[Date] => Sat, 29 May 2004 12:28:14 GMT
[Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)
[Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
[ETag] => "3f80f-1b6-3e1cb03b"
[Accept-Ranges] => bytes
[Content-Length] => 438
[Connection] => close
[Content-Type] => text/html
)
Example #2
get_headers(3) using HEAD example
<?php
// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
array(
'http' => array(
'method' => 'HEAD'
)
)
);
$headers = get_headers('http://example.com');
?>
SEE ALSO apache_request_headers(3).
PHP Documentation Group GET_HEADERS(3)
Check Out this Related Man Page
Plack::Response(3pm) User Contributed Perl Documentation Plack::Response(3pm)NAME
Plack::Response - Portable HTTP Response object for PSGI response
SYNOPSIS
use Plack::Response;
sub psgi_handler {
my $env = shift;
my $res = Plack::Response->new(200);
$res->content_type('text/html');
$res->body("Hello World");
return $res->finalize;
}
DESCRIPTION
Plack::Response allows you a way to create PSGI response array ref through a simple API.
METHODS
new
$res = Plack::Response->new;
$res = Plack::Response->new($status);
$res = Plack::Response->new($status, $headers);
$res = Plack::Response->new($status, $headers, $body);
Creates a new Plack::Response object.
status
$res->status(200);
$status = $res->status;
Sets and gets HTTP status code. "code" is an alias.
headers
$headers = $res->headers;
$res->headers([ 'Content-Type' => 'text/html' ]);
$res->headers({ 'Content-Type' => 'text/html' });
$res->headers( HTTP::Headers->new );
Sets and gets HTTP headers of the response. Setter can take either an array ref, a hash ref or HTTP::Headers object containing a list
of headers.
body
$res->body($body_str);
$res->body([ "Hello", "World" ]);
$res->body($io);
Gets and sets HTTP response body. Setter can take either a string, an array ref, or an IO::Handle-like object. "content" is an alias.
Note that this method doesn't automatically set Content-Length for the response. You have to set it manually if you want, with the
"content_length" method (see below).
header
$res->header('X-Foo' => 'bar');
my $val = $res->header('X-Foo');
Shortcut for "$res->headers->header".
content_type, content_length, content_encoding
$res->content_type('text/plain');
$res->content_length(123);
$res->content_encoding('gzip');
Shortcut for the equivalent get/set methods in "$res->headers".
redirect
$res->redirect($url);
$res->redirect($url, 301);
Sets redirect URL with an optional status code, which defaults to 302.
Note that this method doesn't normalize the given URI string. Users of this module have to be responsible about properly encoding URI
paths and parameters.
location
Gets and sets "Location" header.
Note that this method doesn't normalize the given URI string in the setter. See above in "redirect" for details.
cookies
$res->cookies->{foo} = 123;
$res->cookies->{foo} = { value => '123' };
Returns a hash reference containing cookies to be set in the response. The keys of the hash are the cookies' names, and their
corresponding values are a plain string (for "value" with everything else defaults) or a hash reference that can contain keys such as
"value", "domain", "expires", "path", "httponly", "secure".
"expires" can take a string or an integer (as an epoch time) and does not convert string formats such as "+3M".
$res->cookies->{foo} = {
value => 'test',
path => "/",
domain => '.example.com',
expires => time + 24 * 60 * 60,
};
finalize
$res->finalize;
Returns the status code, headers, and body of this response as a PSGI response array reference.
AUTHOR
Tokuhiro Matsuno
Tatsuhiko Miyagawa
SEE ALSO
Plack::Request
perl v5.14.2 2012-06-21 Plack::Response(3pm)