<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/context.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'context.http.php',
    1 => 'HTTP context options',
    2 => 'HTTP context option listing',
  ),
  'up' => 
  array (
    0 => 'context.php',
    1 => 'Context options and parameters',
  ),
  'prev' => 
  array (
    0 => 'context.socket.php',
    1 => 'Socket context options',
  ),
  'next' => 
  array (
    0 => 'context.ftp.php',
    1 => 'FTP context options',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/context/http.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="context.http" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">HTTP context options</h1>
  <p class="refpurpose"><span class="refname">HTTP context options</span> &mdash; <span class="dc-title">HTTP context option listing</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-context.http-description">
  <h3 class="title">Description</h3>
  <p class="para">
   Context options for <code class="literal">http://</code> and <code class="literal">https://</code>
   transports.
  </p>
 </div>


 <div class="refsect1 options" id="refsect1-context.http-options">
  <h3 class="title">Options</h3>
  <p class="para">
   <dl>
    
     <dt id="context.http.method">
      <code class="parameter">method</code>
      <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
     </dt>
     <dd>
      <p class="para">
       <strong><code>GET</code></strong>, <strong><code>POST</code></strong>, or
       any other HTTP method supported by the remote server.
      </p>
      <p class="para">
       Defaults to <strong><code>GET</code></strong>.
      </p>
     </dd>
    
    
     <dt id="context.http.header">
      <code class="parameter">header</code>
      <span class="type"><a href="language.types.array.php" class="type array">array</a></span> or <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
     </dt>
     <dd>
      <p class="para">
       Additional headers to be sent during request. Values
       in this option will override other values (such as
       <code class="literal">User-agent:</code>, <code class="literal">Host:</code>,
       and <code class="literal">Authentication:</code>),
       even when following <code class="literal">Location:</code> redirects.
       Thus it is not recommended to set a <code class="literal">Host:</code> header,
       if <code class="parameter">follow_location</code> is enabled.
      </p>
      <p class="para">
       String value should be <code class="literal">Key: value</code> pairs delimited by
       <code class="literal">\r\n</code>, e.g.
       <code class="literal">&quot;Content-Type: application/json\r\nConnection: close&quot;</code>.
       Array value should be a list of <code class="literal">Key: value</code> pairs, e.g.
       <code class="literal">[&quot;Content-Type: application/json&quot;, &quot;Connection: close&quot;]</code>.
      </p>
     </dd>
    
    
     <dt id="context.http.user-agent">
      <code class="parameter">user_agent</code>
      <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
     </dt>
     <dd>
      <p class="para">
       Value to send with <code class="literal">User-Agent:</code> header. This value will
       only be used if user-agent is <em>not</em> specified
       in the <code class="literal">header</code> context option above.
      </p>
      <p class="para">
       By default the
       <a href="filesystem.configuration.php#ini.user-agent" class="link">user_agent</a>
       <var class="filename">php.ini</var> setting is used.
      </p>
     </dd>
    
    
     <dt id="context.http.content">
      <code class="parameter">content</code>
      <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
     </dt>
     <dd>
      <p class="para">
       Additional data to be sent after the headers. Typically used
       with POST or PUT requests.
      </p>
     </dd>
    
    
     <dt id="context.http.proxy">
      <code class="parameter">proxy</code>
      <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
     </dt>
     <dd>
      <p class="para">
       URI specifying address of proxy server (e.g.
       <code class="literal">tcp://proxy.example.com:5100</code>).
      </p>
     </dd>
    
    
     <dt id="context.http.request-fulluri">
      <code class="parameter">request_fulluri</code>
      <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>
     </dt>
     <dd>
      <p class="para">
       When set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, the entire URI will be used when
       constructing the request (e.g.
       <code class="literal">GET http://www.example.com/path/to/file.html HTTP/1.0</code>).
       While this is a non-standard request format, some
       proxy servers require it.
      </p>
      <p class="para">
       Defaults to <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
      </p>
     </dd>
    
    
     <dt id="context.http.follow-location">
      <code class="parameter">follow_location</code>
      <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
     </dt>
     <dd>
      <p class="para">
       Follow <code class="literal">Location</code> header redirects. Set to
       <code class="literal">0</code> to disable.
      </p>
      <p class="para">
       Defaults to <code class="literal">1</code>.
      </p>
     </dd>
    
    
     <dt id="context.http.max-redirects">
      <code class="parameter">max_redirects</code>
      <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
     </dt>
     <dd>
      <p class="para">
       The max number of redirects to follow. Value <code class="literal">1</code> or
       less means that no redirects are followed.
      </p>
      <p class="para">
       Defaults to <code class="literal">20</code>.
      </p>
     </dd>
    
    
     <dt id="context.http.protocol-version">
      <code class="parameter">protocol_version</code>
      <span class="type"><a href="language.types.float.php" class="type float">float</a></span>
     </dt>
     <dd>
      <p class="para">
       HTTP protocol version.
      </p>
      <p class="para">
       Defaults to <code class="literal">1.1</code> as of PHP 8.0.0;
       prior to that version the default was <code class="literal">1.0</code>.
      </p>
     </dd>
    
    
     <dt id="context.http.timeout">
      <code class="parameter">timeout</code>
      <span class="type"><a href="language.types.float.php" class="type float">float</a></span>
     </dt>
     <dd>
      <p class="para">
       Read timeout in seconds, specified by a <span class="type"><a href="language.types.float.php" class="type float">float</a></span>
       (e.g. <code class="literal">10.5</code>).
      </p>
      <p class="para">
       By default the
       <a href="filesystem.configuration.php#ini.default-socket-timeout" class="link">default_socket_timeout</a>
       <var class="filename">php.ini</var> setting is used.
      </p>
     </dd>
    
    
     <dt id="context.http.ignore-errors">
      <code class="parameter">ignore_errors</code>
      <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>
     </dt>
     <dd>
      <p class="para">
       Fetch the content even on failure status codes.
      </p>
      <p class="para">
       Defaults to <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-context.http-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="context.http.example-post">
    <p><strong>Example #1 Fetch a page and send POST data</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$postdata </span><span style="color: #007700">= </span><span style="color: #0000BB">http_build_query</span><span style="color: #007700">(<br />    [<br />        </span><span style="color: #DD0000">'var1' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'some content'</span><span style="color: #007700">,<br />        </span><span style="color: #DD0000">'var2' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'doh'</span><span style="color: #007700">,<br />    ]<br />);<br /><br /></span><span style="color: #0000BB">$opts </span><span style="color: #007700">= [<br />    </span><span style="color: #DD0000">'http' </span><span style="color: #007700">=&gt; [<br />        </span><span style="color: #DD0000">'method'  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'POST'</span><span style="color: #007700">,<br />        </span><span style="color: #DD0000">'header'  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Content-type: application/x-www-form-urlencoded'</span><span style="color: #007700">,<br />        </span><span style="color: #DD0000">'content' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$postdata</span><span style="color: #007700">,<br />    ]<br />];<br /><br /></span><span style="color: #0000BB">$context </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://example.com/submit.php'</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="context.http.example-fetch-ignore-redirect">
    <p><strong>Example #2 Ignore redirects but fetch headers and content</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$url </span><span style="color: #007700">= </span><span style="color: #DD0000">"http://www.example.org/header.php"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$opts </span><span style="color: #007700">= [<br />    </span><span style="color: #DD0000">'http' </span><span style="color: #007700">=&gt; [<br />        </span><span style="color: #DD0000">'method'        </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'GET'</span><span style="color: #007700">,<br />        </span><span style="color: #DD0000">'max_redirects' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'0'</span><span style="color: #007700">,<br />        </span><span style="color: #DD0000">'ignore_errors' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'1'</span><span style="color: #007700">,<br />    ]<br />];<br /><br /></span><span style="color: #0000BB">$context </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stream </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">, </span><span style="color: #DD0000">'r'</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// header information as well as meta data<br />// about the stream<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">stream_get_meta_data</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// actual data at $url<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-context.http-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <strong>Underlying socket stream context options</strong><br />
   <span class="simpara">
    Additional context options may be supported by the
    <a href="transports.inet.php" class="link">underlying transport</a>
    For <code class="literal">http://</code> streams, refer to context
    options for the <code class="literal">tcp://</code> transport.  For
    <code class="literal">https://</code> streams, refer to context options
    for the <code class="literal">ssl://</code> transport.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <strong>HTTP status line</strong><br />
   <span class="simpara">
    When this stream wrapper follows a redirect, the
    <code class="literal">wrapper_data</code> returned by
    <span class="function"><a href="function.stream-get-meta-data.php" class="function">stream_get_meta_data()</a></span> might not necessarily contain
    the HTTP status line that actually applies to the content data at index
    <code class="literal">0</code>.
   </span>
   <div class="example-contents screen">
<div class="notescode"><pre class="notescode">array (
  &#039;wrapper_data&#039; =&gt;
  array (
    0 =&gt; &#039;HTTP/1.0 301 Moved Permanently&#039;,
    1 =&gt; &#039;Cache-Control: no-cache&#039;,
    2 =&gt; &#039;Connection: close&#039;,
    3 =&gt; &#039;Location: http://example.com/foo.jpg&#039;,
    4 =&gt; &#039;HTTP/1.1 200 OK&#039;,
    ...</pre>
</div>
   </div>
   <span class="simpara">
    The first request returned a <code class="literal">301</code> (permanent redirect),
    so the stream wrapper automatically followed the redirect to get a
    <code class="literal">200</code> response (index = <code class="literal">4</code>).
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-context.http-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><a href="wrappers.http.php" class="xref">http://</a></li>
    <li><a href="context.socket.php" class="xref">Socket context options</a></li>
    <li><a href="context.ssl.php" class="xref">SSL context options</a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>