<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >fsockopen</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="Network Functions" HREF="ref.network.html"><LINK REL="PREVIOUS" TITLE="dns_get_record" HREF="function.dns-get-record.html"><LINK REL="NEXT" TITLE="gethostbyaddr" HREF="function.gethostbyaddr.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-2"></HEAD ><BODY CLASS="refentry" 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 PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.dns-get-record.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.gethostbyaddr.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.fsockopen" ></A >fsockopen</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN74057" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >fsockopen -- Open Internet or Unix domain socket connection </DIV ><DIV CLASS="refsect1" ><A NAME="AEN74060" ></A ><H2 >Description</H2 >resource <B CLASS="methodname" >fsockopen</B > ( string target, int port [, int errno [, string errstr [, float timeout]]])<BR ></BR ><P > Initiates a socket connection to the resource specified by <VAR CLASS="parameter" >target</VAR >. <VAR CLASS="literal" >PHP</VAR > supports targets in the Internet and Unix domains as described in <A HREF="transports.html" >Anexa N</A >. A list of supported transports can also be retrieved using <A HREF="function.stream-get-transports.html" ><B CLASS="function" >stream_get_transports()</B ></A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > If you need to set a timeout for reading/writing data over the socket, use <A HREF="function.stream-set-timeout.html" ><B CLASS="function" >stream_set_timeout()</B ></A >, as the <VAR CLASS="parameter" >timeout</VAR > parameter to <B CLASS="function" >fsockopen()</B > only applies while connecting the socket. </P ></BLOCKQUOTE ></DIV ><P > As of PHP 4.3.0, if you have compiled in OpenSSL support, you may prefix the <VAR CLASS="parameter" >hostname</VAR > with either '<VAR CLASS="literal" >ssl://</VAR >' or '<VAR CLASS="literal" >tls://</VAR >' to use an SSL or TLS client connection over TCP/IP to connect to the remote host. </P ><P > <B CLASS="function" >fsockopen()</B > returns a file pointer which may be used together with the other file functions (such as <A HREF="function.fgets.html" ><B CLASS="function" >fgets()</B ></A >, <A HREF="function.fgetss.html" ><B CLASS="function" >fgetss()</B ></A >, <A HREF="function.fwrite.html" ><B CLASS="function" >fwrite()</B ></A >, <A HREF="function.fclose.html" ><B CLASS="function" >fclose()</B ></A >, and <A HREF="function.feof.html" ><B CLASS="function" >feof()</B ></A >). </P ><P > If the call fails, it will return <TT CLASS="constant" ><B >FALSE</B ></TT > and if the optional <VAR CLASS="parameter" >errno</VAR > and <VAR CLASS="parameter" >errstr</VAR > arguments are present they will be set to indicate the actual system level error that occurred in the system-level <VAR CLASS="literal" >connect()</VAR > call. If the value returned in <VAR CLASS="parameter" >errno</VAR > is <VAR CLASS="literal" >0</VAR > and the function returned <TT CLASS="constant" ><B >FALSE</B ></TT >, it is an indication that the error occurred before the <VAR CLASS="literal" >connect()</VAR > call. This is most likely due to a problem initializing the socket. Note that the <VAR CLASS="parameter" >errno</VAR > and <VAR CLASS="parameter" >errstr</VAR > arguments will always be passed by reference. </P ><P > Depending on the environment, the Unix domain or the optional connect timeout may not be available. </P ><P > The socket will by default be opened in blocking mode. You can switch it to non-blocking mode by using <A HREF="function.stream-set-blocking.html" ><B CLASS="function" >stream_set_blocking()</B ></A >. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN74115" ></A ><P ><B >Exemplu 1. <B CLASS="function" >fsockopen()</B > Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$fp </font><font color="#007700">= </font><font color="#0000BB">fsockopen</font><font color="#007700">(</font><font color="#DD0000">"www.example.com"</font><font color="#007700">, </font><font color="#0000BB">80</font><font color="#007700">, </font><font color="#0000BB">$errno</font><font color="#007700">, </font><font color="#0000BB">$errstr</font><font color="#007700">, </font><font color="#0000BB">30</font><font color="#007700">);<br />if (!</font><font color="#0000BB">$fp</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"$errstr ($errno)<br /></font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />} else {<br /> </font><font color="#0000BB">$out </font><font color="#007700">= </font><font color="#DD0000">"GET / HTTP/1.1\r\n"</font><font color="#007700">;<br /> </font><font color="#0000BB">$out </font><font color="#007700">.= </font><font color="#DD0000">"Host: www.example.com\r\n"</font><font color="#007700">;<br /> </font><font color="#0000BB">$out </font><font color="#007700">.= </font><font color="#DD0000">"Connection: Close\r\n\r\n"</font><font color="#007700">;<br /><br /> </font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#0000BB">$out</font><font color="#007700">);<br /> while (!</font><font color="#0000BB">feof</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">)) {<br /> echo </font><font color="#0000BB">fgets</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#0000BB">128</font><font color="#007700">);<br /> }<br /> </font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > The example below shows how to retrieve the day and time from the UDP service "daytime" (port 13) in your own machine. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN74119" ></A ><P ><B >Exemplu 2. Using UDP connection</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$fp </font><font color="#007700">= </font><font color="#0000BB">fsockopen</font><font color="#007700">(</font><font color="#DD0000">"udp://127.0.0.1"</font><font color="#007700">, </font><font color="#0000BB">13</font><font color="#007700">, </font><font color="#0000BB">$errno</font><font color="#007700">, </font><font color="#0000BB">$errstr</font><font color="#007700">);<br />if (!</font><font color="#0000BB">$fp</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"ERROR: $errno - $errstr<br /></font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />} else {<br /> </font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#DD0000">"\n"</font><font color="#007700">);<br /> echo </font><font color="#0000BB">fread</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#0000BB">26</font><font color="#007700">);<br /> </font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Avertisment</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data. </P ></TD ></TR ></TABLE ></DIV > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B >When specifying a numerical IPv6 address (e.g. fe80::1) you must enclose the IP in square brackets. For example, <VAR CLASS="literal" >tcp://[fe80::1]:80</VAR >.</P ></BLOCKQUOTE ></DIV > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B >The timeout parameter was introduced in PHP 3.0.9 and UDP support was added in PHP 4. </P ></BLOCKQUOTE ></DIV > See also <A HREF="function.pfsockopen.html" ><B CLASS="function" >pfsockopen()</B ></A >, <A HREF="function.stream-set-blocking.html" ><B CLASS="function" >stream_set_blocking()</B ></A >, <A HREF="function.stream-set-timeout.html" ><B CLASS="function" >stream_set_timeout()</B ></A >, <A HREF="function.fgets.html" ><B CLASS="function" >fgets()</B ></A >, <A HREF="function.fgetss.html" ><B CLASS="function" >fgetss()</B ></A >, <A HREF="function.fwrite.html" ><B CLASS="function" >fwrite()</B ></A >, <A HREF="function.fclose.html" ><B CLASS="function" >fclose()</B ></A >, <A HREF="function.feof.html" ><B CLASS="function" >feof()</B ></A >, and the <A HREF="ref.curl.html" >Curl extension</A >. </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="function.dns-get-record.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.gethostbyaddr.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >dns_get_record</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.network.html" ACCESSKEY="U" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >gethostbyaddr</TD ></TR ></TABLE ></DIV ></BODY ></HTML >