<!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 NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="Manual do PHP" HREF="index.html"><LINK REL="UP" TITLE="List of Supported Protocols/Wrappers" HREF="wrappers.html"><LINK REL="PREVIOUS" TITLE="Socket" HREF="wrappers.socket.html"><LINK REL="NEXT" TITLE="FTP and FTPS" HREF="wrappers.ftp.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="section" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual do PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="wrappers.socket.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Apêndice N. List of Supported Protocols/Wrappers</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="wrappers.ftp.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="wrappers.http" >HTTP and HTTPS</A ></H1 ><P >PHP 3, PHP 4, PHP 5. <TT CLASS="filename" >https://</TT > since PHP 4.3.0</P ><P ></P ><UL ><LI ><P ><TT CLASS="filename" >http://example.com</TT ></P ></LI ><LI ><P ><TT CLASS="filename" >http://example.com/file.php?var1=val1&var2=val2</TT ></P ></LI ><LI ><P ><TT CLASS="filename" >http://user:password@example.com</TT ></P ></LI ><LI ><P ><TT CLASS="filename" >https://example.com</TT ></P ></LI ><LI ><P ><TT CLASS="filename" >https://example.com/file.php?var1=val1&var2=val2</TT ></P ></LI ><LI ><P ><TT CLASS="filename" >https://user:password@example.com</TT ></P ></LI ></UL ><P >Allows read-only access to files/resources via HTTP 1.0, using the HTTP GET method. A <TT CLASS="literal" >Host:</TT > header is sent with the request to handle name-based virtual hosts. If you have configured a <A HREF="ref.filesystem.html#ini.user-agent" >user_agent</A > string using your ini file or the stream context, it will also be included in the request. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Atenção</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >Quando usando SSL, o Microsoft IIS irá violar o protocolo fechando a conexão sem enviar uma notificação close_notify. O PHP acusará isso como sendo "SSL: Fatal Protocol Error" quando tentar ler os dados. Para prevenir isso, você deve baixar seu <A HREF="ref.errorfunc.html#ini.error-reporting" >error_reporting</A > para o nível que não emita warnings. O PHP 4.3.7 e seguintes conseguem detectar servidores IIS defeituosos quando você abre um stream utilizando o wrapper https:// e suprimirá a mensagem de warning para você. Se você está usando <A HREF="function.fsockopen.html" ><B CLASS="function" >fsockopen()</B ></A > para criar um socket ssl://, a responsabilidade de detectar e suprimir esse warning passa para você. </P ></TD ></TR ></TABLE ></DIV ><P > 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" ><P ></P ><A NAME="AEN288331" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$url </font><font color="#007700">= </font><font color="#DD0000">'http://www.example.com/redirecting_page.php'</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#0000BB">$url</font><font color="#007700">, </font><font color="#DD0000">'r'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Prior to PHP 4.3.0 use $http_response_header <br /> instead of stream_get_meta_data() */<br /></font><font color="#0000BB">$meta_data </font><font color="#007700">= </font><font color="#0000BB">stream_get_meta_data</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br />foreach(</font><font color="#0000BB">$meta_data</font><font color="#007700">[</font><font color="#DD0000">'wrapper_data'</font><font color="#007700">] as </font><font color="#0000BB">$response</font><font color="#007700">) {<br /><br /> </font><font color="#FF8000">/* Were we redirected? */<br /> </font><font color="#007700">if (</font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#0000BB">strtolower</font><font color="#007700">(</font><font color="#0000BB">$response</font><font color="#007700">), </font><font color="#0000BB">0</font><font color="#007700">, </font><font color="#0000BB">10</font><font color="#007700">) == </font><font color="#DD0000">'location: '</font><font color="#007700">) {<br /> </font><font color="#FF8000">/* update $url with where we were redirected to */<br /> </font><font color="#0000BB">$url </font><font color="#007700">= </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#0000BB">$response</font><font color="#007700">, </font><font color="#0000BB">18</font><font color="#007700">);<br /> }<br /><br />}<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN288333" ></A ><P ><B >Exemplo N-2. Fetch a page and send POST data</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br />$postdata </font><font color="#007700">= </font><font color="#0000BB">http_build_query</font><font color="#007700">(<br /> array(<br /> </font><font color="#DD0000">'var1' </font><font color="#007700">=> </font><font color="#DD0000">'some content'</font><font color="#007700">,<br /> </font><font color="#DD0000">'var2' </font><font color="#007700">=> </font><font color="#DD0000">'doh'<br /> </font><font color="#007700">)<br />);<br /><br /></font><font color="#0000BB">$opts </font><font color="#007700">= array(</font><font color="#DD0000">'http' </font><font color="#007700">=><br /> array(<br /> </font><font color="#DD0000">'method' </font><font color="#007700">=> </font><font color="#DD0000">'POST'</font><font color="#007700">,<br /> </font><font color="#DD0000">'header' </font><font color="#007700">=> </font><font color="#DD0000">'Content-type: application/x-www-form-urlencoded'</font><font color="#007700">,<br /> </font><font color="#DD0000">'content' </font><font color="#007700">=> </font><font color="#0000BB">$postdata<br /> </font><font color="#007700">)<br />);<br /><br /></font><font color="#0000BB">$context </font><font color="#007700">= </font><font color="#0000BB">stream_context_create</font><font color="#007700">(</font><font color="#0000BB">$opts</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">file_get_contents</font><font color="#007700">(</font><font color="#DD0000">'http://example.com/submit.php'</font><font color="#007700">, </font><font color="#0000BB">false</font><font color="#007700">, </font><font color="#0000BB">$context</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > The stream allows access to the <SPAN CLASS="emphasis" ><I CLASS="emphasis" >body</I ></SPAN > of the resource; the headers are stored in the <CODE CLASS="varname" >$http_response_header</CODE > variable. Since PHP 4.3.0, the headers are available using <A HREF="function.stream-get-meta-data.html" ><B CLASS="function" >stream_get_meta_data()</B ></A >. </P ><P > HTTP connections are read-only; you cannot write data or copy files to an HTTP resource. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Nota: </B >HTTPS is supported starting from PHP 4.3.0, if you have compiled in support for OpenSSL. </P ></BLOCKQUOTE ></DIV ><P > <DIV CLASS="table" ><A NAME="AEN288344" ></A ><P ><B >Tabela N-2. Wrapper Summary</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Attribute</TH ><TH >Supported</TH ></TR ></THEAD ><TBODY ><TR ><TD >Restricted by <A HREF="ref.filesystem.html#ini.allow-url-fopen" >allow_url_fopen</A ></TD ><TD >Yes</TD ></TR ><TR ><TD >Allows Reading</TD ><TD >Yes</TD ></TR ><TR ><TD >Allows Writing</TD ><TD >No</TD ></TR ><TR ><TD >Allows Appending</TD ><TD >No</TD ></TR ><TR ><TD >Allows Simultaneous Reading and Writing</TD ><TD >N/A</TD ></TR ><TR ><TD >Supports <A HREF="function.stat.html" ><B CLASS="function" >stat()</B ></A ></TD ><TD >No</TD ></TR ><TR ><TD >Supports <A HREF="function.unlink.html" ><B CLASS="function" >unlink()</B ></A ></TD ><TD >No</TD ></TR ><TR ><TD >Supports <A HREF="function.rename.html" ><B CLASS="function" >rename()</B ></A ></TD ><TD >No</TD ></TR ><TR ><TD >Supports <A HREF="function.mkdir.html" ><B CLASS="function" >mkdir()</B ></A ></TD ><TD >No</TD ></TR ><TR ><TD >Supports <A HREF="function.rmdir.html" ><B CLASS="function" >rmdir()</B ></A ></TD ><TD >No</TD ></TR ></TBODY ></TABLE ></DIV > </P ><P > <DIV CLASS="table" ><A NAME="AEN288389" ></A ><P ><B >Tabela N-3. Context options</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Name</TH ><TH >Usage</TH ><TH >Default</TH ></TR ></THEAD ><TBODY ><TR ><TD ><TT CLASS="literal" >method</TT ></TD ><TD > <TT CLASS="constant" ><B >GET</B ></TT >, <TT CLASS="constant" ><B >POST</B ></TT >, or any other HTTP method supported by the remote server. </TD ><TD ><TT CLASS="constant" ><B >GET</B ></TT ></TD ></TR ><TR ><TD ><TT CLASS="literal" >header</TT ></TD ><TD >Additional headers to be sent during request. Values in this option will override other values (such as <TT CLASS="literal" >User-agent:</TT >, <TT CLASS="literal" >Host:</TT >, and <TT CLASS="literal" >Authentication:</TT >). </TD ><TD > </TD ></TR ><TR ><TD ><TT CLASS="literal" >user_agent</TT ></TD ><TD >Value to send with User-Agent: header. This value will only be used if user-agent is <SPAN CLASS="emphasis" ><I CLASS="emphasis" >not</I ></SPAN > specified in the <TT CLASS="literal" >header</TT > context option above. </TD ><TD > <TT CLASS="filename" >php.ini</TT > setting: <TT CLASS="literal" >user_agent</TT > </TD ></TR ><TR ><TD ><TT CLASS="literal" >content</TT ></TD ><TD > Additional data to be sent after the headers. Typically used with POST or PUT requests. </TD ><TD > </TD ></TR ><TR ><TD ><TT CLASS="literal" >proxy</TT ></TD ><TD > URI specifying address of proxy server. (e.g. <TT CLASS="literal" >tcp://proxy.example.com:5100</TT >). HTTPS proxying (through HTTP proxies) only works in PHP 5.1.0 or greater. </TD ><TD > </TD ></TR ><TR ><TD ><TT CLASS="literal" >request_fulluri</TT ></TD ><TD > When set to <TT CLASS="constant" ><B >TRUE</B ></TT >, the entire URI will be used when constructing the request. (i.e. <TT CLASS="literal" >GET http://www.example.com/path/to/file.html HTTP/1.0</TT >). While this is a non-standard request format, some proxy servers require it. </TD ><TD ><TT CLASS="constant" ><B >FALSE</B ></TT ></TD ></TR ><TR ><TD ><TT CLASS="literal" >max_redirects</TT ></TD ><TD > The max number of redirects to follow. Value <TT CLASS="literal" >1</TT > or less means that no redirects are followed. Added in PHP 5.1.0. </TD ><TD >20</TD ></TR ><TR ><TD ><TT CLASS="literal" >protocol_version</TT ></TD ><TD > HTTP protocol version. Added in PHP 5.1.0. </TD ><TD >1.0</TD ></TR ><TR ><TD ><TT CLASS="literal" >timeout</TT ></TD ><TD > Read timeout in seconds, specified by a <A HREF="language.types.float.html" ><B CLASS="type" >float</B ></A > (e.g. <TT CLASS="literal" >10.5</TT >). Added in PHP 5.2.1. </TD ><TD ><A HREF="ref.filesystem.html#ini.default-socket-timeout" >default_socket_timeout</A ></TD ></TR ></TBODY ></TABLE ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Underlying socket stream context options: </B > Additional context options may be supported by the <A HREF="transports.html#transports.inet" >underlying transport</A > For <TT CLASS="literal" >http://</TT > streams, refer to context options for the <TT CLASS="literal" >tcp://</TT > transport. For <TT CLASS="literal" >https://</TT > streams, refer to context options for the <TT CLASS="literal" >ssl://</TT > transport. </P ></BLOCKQUOTE ></DIV ><P > 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 <TT CLASS="literal" >user_agent</TT > INI setting. Set <TT CLASS="literal" >user_agent</TT > to any valid string (such as the default <TT CLASS="literal" >PHP/version</TT > 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 > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN288474" ></A ><P ><B >Exemplo N-3. Sending custom headers with an HTTP request</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />ini_set</font><font color="#007700">(</font><font color="#DD0000">'user_agent'</font><font color="#007700">, </font><font color="#DD0000">"PHP\r\nX-MyCustomHeader: Foo"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">'http://www.example.com/index.php'</font><font color="#007700">, </font><font color="#DD0000">'r'</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >Results in the following request being sent:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >GET /index.php HTTP/1.0 Host: www.example.com User-Agent: PHP X-MyCustomHeader: Foo</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="wrappers.socket.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Principal</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="wrappers.ftp.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Socket</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="wrappers.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >FTP and FTPS</TD ></TR ></TABLE ></DIV ></BODY ></HTML >