<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Fetches all the headers sent by the server in response to a HTTP request</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.base64-encode.html">base64_encode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.get-meta-tags.html">get_meta_tags</a></div> <div class="up"><a href="ref.url.html">URL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.get-headers" class="refentry"> <div class="refnamediv"> <h1 class="refname">get_headers</h1> <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">get_headers</span> — <span class="dc-title">Fetches all the headers sent by the server in response to a HTTP request</span></p> </div> <div class="refsect1 description" id="refsect1-function.get-headers-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>get_headers</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$url</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$format</code><span class="initializer"> = 0</span></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>get_headers()</strong></span> returns an array with the headers sent by the server in response to a HTTP request. </p> </div> <div class="refsect1 parameters" id="refsect1-function.get-headers-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">url</code></em></span> <dd> <p class="para"> The target URL. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">format</code></em></span> <dd> <p class="para"> If the optional <em><code class="parameter">format</code></em> parameter is set to non-zero, <span class="function"><strong>get_headers()</strong></span> parses the response and sets the array's keys. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.get-headers-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an indexed or associative array with the headers, or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 changelog" id="refsect1-function.get-headers-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.1.3</td> <td> This function now uses the default stream context, which can be set/changed with the <span class="function"><a href="function.stream-context-set-default.html" class="function">stream_context_set_default()</a></span> function. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.get-headers-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4081"> <p><strong>Example #1 <span class="function"><strong>get_headers()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$url </span><span style="color: #007700">= </span><span style="color: #DD0000">'http://www.example.com'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">get_headers</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">get_headers</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 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 ) </pre></div> </div> </div> <div class="example" id="example-4082"> <p><strong>Example #2 <span class="function"><strong>get_headers()</strong></span> using HEAD example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// By default get_headers uses a GET request to fetch the headers. If you<br />// want to send a HEAD request instead, you can do so using a stream context:<br /></span><span style="color: #0000BB">stream_context_set_default</span><span style="color: #007700">(<br /> array(<br /> </span><span style="color: #DD0000">'http' </span><span style="color: #007700">=> array(<br /> </span><span style="color: #DD0000">'method' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'HEAD'<br /> </span><span style="color: #007700">)<br /> )<br />);<br /></span><span style="color: #0000BB">$headers </span><span style="color: #007700">= </span><span style="color: #0000BB">get_headers</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://example.com'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.get-headers-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.apache-request-headers.html" class="function" rel="rdfs-seeAlso">apache_request_headers()</a> - Fetch all HTTP request headers</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.base64-encode.html">base64_encode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.get-meta-tags.html">get_meta_tags</a></div> <div class="up"><a href="ref.url.html">URL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>