wrappers.http.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 184 行

HTML
184
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>HTTP and HTTPS</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="wrappers.html">List of Supported Protocols/Wrappers</a></div> <div class="next" style="text-align: right; float: right;"><a href="wrappers.ftp.html">FTP and FTPS</a></div> <div class="up"><a href="wrappers.html">List of Supported Protocols/Wrappers</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><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&amp;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&amp;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.html#ini.user-agent" class="link">user_agent</a> string using   your ini 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 IISwill violate the protocol by closing the connection without sending a<i>close_notify</i> indicator. PHP will report this as &quot;SSL: FatalProtocol Error&quot; when you reach the end of the data. To work around this, thevalue of <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> should belowered to a level that does not include warnings.PHP 4.3.7 and higher can detect buggy IIS server software when you openthe stream using the <i>https://</i> wrapper and will suppress thewarning. When using <a href="function.fsockopen.html" class="function">fsockopen()</a> to create an<i>ssl://</i> socket, the developer is responsible for detectingand 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&#039;s important to know the URL of the resource where   your document came from (after all redirects have been processed),   you&#039;ll need to process the series of response headers returned by the   stream.  </p>  <div class="informalexample">   <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$url&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Prior&nbsp;to&nbsp;PHP&nbsp;4.3.0&nbsp;use&nbsp;$http_response_header<br />&nbsp;&nbsp;&nbsp;instead&nbsp;of&nbsp;stream_get_meta_data()&nbsp;*/<br /></span><span style="color: #0000BB">$meta_data&nbsp;</span><span style="color: #007700">=&nbsp;</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">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$response</span><span style="color: #007700">)&nbsp;{<br /><br />&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Were&nbsp;we&nbsp;redirected?&nbsp;*/<br />&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</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">),&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #DD0000">'location:&nbsp;'</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;update&nbsp;$url&nbsp;with&nbsp;where&nbsp;we&nbsp;were&nbsp;redirected&nbsp;to&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$response</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">18</span><span style="color: #007700">);<br />&nbsp;&nbsp;}<br /><br />}<br /><br /></span><span style="color: #0000BB">?&gt;</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.html" 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.html" 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="para">   <table border="5">    <caption><b>Wrapper Summary</b></caption>    <colgroup>     <thead valign="middle">      <tr valign="middle">       <th colspan="1">Attribute</th>       <th colspan="1">Supported</th>      </tr>     </thead>     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">Restricted by <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">allow_url_fopen</a></td>       <td colspan="1" rowspan="1" align="left">Yes</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">Allows Reading</td>       <td colspan="1" rowspan="1" align="left">Yes</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">Allows Writing</td>       <td colspan="1" rowspan="1" align="left">No</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">Allows Appending</td>       <td colspan="1" rowspan="1" align="left">No</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">Allows Simultaneous Reading and Writing</td>       <td colspan="1" rowspan="1" align="left">N/A</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">Supports <a href="function.stat.html" class="function">stat()</a></td>       <td colspan="1" rowspan="1" align="left">No</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">Supports <a href="function.unlink.html" class="function">unlink()</a></td>       <td colspan="1" rowspan="1" align="left">No</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">Supports <a href="function.rename.html" class="function">rename()</a></td>       <td colspan="1" rowspan="1" align="left">No</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">Supports <a href="function.mkdir.html" class="function">mkdir()</a></td>       <td colspan="1" rowspan="1" align="left">No</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">Supports <a href="function.rmdir.html" class="function">rmdir()</a></td>       <td colspan="1" rowspan="1" align="left">No</td>      </tr>     </tbody>    </colgroup>   </table>  </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">   <div class="example">    <p><b>Example #1 Sending custom headers with an HTTP request</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'user_agent'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"PHP\r\nX-MyCustomHeader:&nbsp;Foo"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>Results in the following request being sent:</p></div>    <div class="example-contents"><pre><div class="cdata"><pre>GET /index.php HTTP/1.0Host: www.example.comUser-Agent: PHPX-MyCustomHeader: Foo</pre></div>    </pre></div>   </div>  </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="wrappers.html">List of Supported Protocols/Wrappers</a></div> <div class="next" style="text-align: right; float: right;"><a href="wrappers.ftp.html">FTP and FTPS</a></div> <div class="up"><a href="wrappers.html">List of Supported Protocols/Wrappers</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?