Source of: /manual/en/wrappers.http.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/wrappers.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'wrappers.http.php',
1 => 'HTTP and HTTPS',
),
'up' =>
array (
0 => 'wrappers.php',
1 => 'List of Supported Protocols/Wrappers',
),
'prev' =>
array (
0 => 'wrappers.file.php',
1 => 'Filesystem',
),
'next' =>
array (
0 => 'wrappers.ftp.php',
1 => 'FTP and FTPS',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="wrappers.http" class="section">
<h2 class="title">HTTP and HTTPS</h2>
<p class="simpara">PHP 4, PHP 5, PHP 6. <var class="filename">https://</var> since PHP 4.3.0</p>
<ul class="itemizedlist">
<li class="listitem"><span class="simpara"><var class="filename">http://example.com</var></span></li>
<li class="listitem"><span class="simpara"><var class="filename">http://example.com/file.php?var1=val1&var2=val2</var></span></li>
<li class="listitem"><span class="simpara"><var class="filename">http://user:password@example.com</var></span></li>
<li class="listitem"><span class="simpara"><var class="filename">https://example.com</var></span></li>
<li class="listitem"><span class="simpara"><var class="filename">https://example.com/file.php?var1=val1&var2=val2</var></span></li>
<li class="listitem"><span class="simpara"><var class="filename">https://user:password@example.com</var></span></li>
</ul>
<p class="simpara">Allows read-only access to files/resources via HTTP 1.0,
using the HTTP GET method. A <i>Host:</i> header is sent with the request
to handle name-based virtual hosts. If you have configured
a <a href="filesystem.configuration.php#ini.user-agent" class="link">user_agent</a> string using
your <var class="filename">php.ini</var> file or the stream context, it will also be included
in the request.
</p>
<div class="warning"><b class="warning">Warning</b><p class="para">When using SSL, Microsoft IIS
will violate the protocol by closing the connection without sending a
<i>close_notify</i> indicator. PHP will report this as "SSL: Fatal
Protocol Error" when you reach the end of the data. To work around this, the
value of <a href="errorfunc.configuration.php#ini.error-reporting" class="link">error_reporting</a> should be
lowered to a level that does not include warnings.
PHP 4.3.7 and higher can detect buggy IIS server software when you open
the stream using the <i>https://</i> wrapper and will suppress the
warning. When using <a href="function.fsockopen.php" class="function">fsockopen()</a> to create an
<i>ssl://</i> socket, the developer is responsible for detecting
and suppressing this warning.</p></div>
<p class="simpara">
Redirects have been supported since PHP 4.0.5; if you are using
an earlier version you will need to include trailing slashes in
your URLs. If it's important to know the URL of the resource where
your document came from (after all redirects have been processed),
you'll need to process the series of response headers returned by the
stream.
</p>
<div class="informalexample">
<div class="example-contents programlisting">
<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/redirecting_page.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$fp </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">);<br /><br /></span><span style="color: #FF8000">/* Prior to PHP 4.3.0 use $http_response_header<br /> instead of stream_get_meta_data() */<br /></span><span style="color: #0000BB">$meta_data </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_get_meta_data</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />foreach(</span><span style="color: #0000BB">$meta_data</span><span style="color: #007700">[</span><span style="color: #DD0000">'wrapper_data'</span><span style="color: #007700">] as </span><span style="color: #0000BB">$response</span><span style="color: #007700">) {<br /><br /> </span><span style="color: #FF8000">/* Were we redirected? */<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">strtolower</span><span style="color: #007700">(</span><span style="color: #0000BB">$response</span><span style="color: #007700">), </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">) == </span><span style="color: #DD0000">'location: '</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/* update $url with where we were redirected to */<br /> </span><span style="color: #0000BB">$url </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$response</span><span style="color: #007700">, </span><span style="color: #0000BB">18</span><span style="color: #007700">);<br /> }<br /><br />}<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div>
<p class="simpara">
The stream allows access to the <em class="emphasis">body</em> of
the resource; the headers are stored in the
<var class="varname"><a href="reserved.variables.httpresponseheader.php" class="classname">$http_response_header</a></var> variable.
Since PHP 4.3.0, the headers are available using
<a href="function.stream-get-meta-data.php" class="function">stream_get_meta_data()</a>.
</p>
<p class="simpara">
HTTP connections are read-only; you cannot write data or copy
files to an HTTP resource.
</p>
<blockquote><p><b class="note">Note</b>:
<span class="simpara">HTTPS is supported starting from PHP 4.3.0, if you
have compiled in support for OpenSSL.
</span>
</p></blockquote>
<p class="simpara">
If you have set the <a href="filesystem.configuration.php#ini.from" class="link">from</a> directive
in <var class="filename">php.ini</var>, and do not define a <i>From:</i> header
in the <a href="context.php" class="xref">Context options and parameters</a>, then this value will be sent as the
<i>From:</i> header in the HTTP request.
</p>
<p class="para">
</p><table class="doctable table">
<caption><b>Wrapper Summary</b></caption>
<thead valign="middle">
<tr valign="middle">
<th>Attribute</th>
<th>Supported</th>
</tr>
</thead>
<tbody valign="middle" class="tbody">
<tr valign="middle">
<td align="left">Restricted by <a href="filesystem.configuration.php#ini.allow-url-fopen" class="link">allow_url_fopen</a></td>
<td align="left">Yes</td>
</tr>
<tr valign="middle">
<td align="left">Allows Reading</td>
<td align="left">Yes</td>
</tr>
<tr valign="middle">
<td align="left">Allows Writing</td>
<td align="left">No</td>
</tr>
<tr valign="middle">
<td align="left">Allows Appending</td>
<td align="left">No</td>
</tr>
<tr valign="middle">
<td align="left">Allows Simultaneous Reading and Writing</td>
<td align="left">N/A</td>
</tr>
<tr valign="middle">
<td align="left">Supports <a href="function.stat.php" class="function">stat()</a></td>
<td align="left">No</td>
</tr>
<tr valign="middle">
<td align="left">Supports <a href="function.unlink.php" class="function">unlink()</a></td>
<td align="left">No</td>
</tr>
<tr valign="middle">
<td align="left">Supports <a href="function.rename.php" class="function">rename()</a></td>
<td align="left">No</td>
</tr>
<tr valign="middle">
<td align="left">Supports <a href="function.mkdir.php" class="function">mkdir()</a></td>
<td align="left">No</td>
</tr>
<tr valign="middle">
<td align="left">Supports <a href="function.rmdir.php" class="function">rmdir()</a></td>
<td align="left">No</td>
</tr>
</tbody>
</table>
<p>
</p>
<p class="para">
Custom headers may be sent with an HTTP request prior to
version 5 by taking advantage of a side-effect in the
handling of the <i>user_agent</i> INI setting.
Set <i>user_agent</i> to any valid string
(such as the default <i>PHP/version</i> setting)
followed by a carriage-return/line-feed pair and any
additional headers.
This method works in PHP 4 and all later versions.
</p>
<p class="para">
</p><div class="example">
<p><b>Example #1 Sending custom headers with an HTTP request</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'user_agent'</span><span style="color: #007700">, </span><span style="color: #DD0000">"PHP\r\nX-MyCustomHeader: Foo"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/index.php'</span><span style="color: #007700">, </span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>Results in the following request being sent:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
GET /index.php HTTP/1.0
Host: www.example.com
User-Agent: PHP
X-MyCustomHeader: Foo
</pre></div>
</div>
</div><p>
</p>
</div><?php manual_footer(); ?>