<HTML ><HEAD ><TITLE >fsockopen</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" 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-1"></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" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.dns-get-record.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.gethostbyaddr.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.fsockopen" ></A >fsockopen</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN60543" ></A ><P > (PHP 3, PHP 4 )</P >fsockopen -- Open Internet or Unix domain socket connection </DIV ><DIV CLASS="refsect1" ><A NAME="AEN60546" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >fsockopen</B > ( string hostname, int port [, int errno [, string errstr [, float timeout]]])<BR ></BR ><P > Initiates a stream connection in the Internet (AF_INET, using TCP or UDP) or Unix (AF_UNIX) domain. For the Internet domain, it will open a TCP socket connection to <TT CLASS="parameter" ><I >hostname</I ></TT > on port <TT CLASS="parameter" ><I >port</I ></TT >. <TT CLASS="parameter" ><I >hostname</I ></TT > may in this case be either a fully qualified domain name or an IP address. For UDP connections, you need to explicitly specify the protocol by prefixing <TT CLASS="parameter" ><I >hostname</I ></TT > with '<TT CLASS="literal" >udp://</TT >'. For the Unix domain, <TT CLASS="parameter" ><I >hostname</I ></TT > will be used as the path to the socket, <TT CLASS="parameter" ><I >port</I ></TT > must be set to 0 in this case. The optional <TT CLASS="parameter" ><I >timeout</I ></TT > can be used to set a timeout in seconds for the connect system call. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > If you need to set a timeout for reading/writing data over the socket, use <A HREF="function.socket-set-timeout.html" ><B CLASS="function" >socket_set_timeout()</B ></A >, as the <TT CLASS="parameter" ><I >timeout</I ></TT > 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 <TT CLASS="parameter" ><I >hostname</I ></TT > with either '<TT CLASS="literal" >ssl://</TT >' or '<TT CLASS="literal" >tls://</TT >' 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.fputs.html" ><B CLASS="function" >fputs()</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 <TT CLASS="parameter" ><I >errno</I ></TT > and <TT CLASS="parameter" ><I >errstr</I ></TT > arguments are present they will be set to indicate the actual system level error that occurred in the system-level <TT CLASS="literal" >connect()</TT > call. If the value returned in <TT CLASS="parameter" ><I >errno</I ></TT > is <TT CLASS="literal" >0</TT > and the function returned <TT CLASS="constant" ><B >FALSE</B ></TT >, it is an indication that the error occurred before the <TT CLASS="literal" >connect()</TT > call. This is most likely due to a problem initializing the socket. Note that the <TT CLASS="parameter" ><I >errno</I ></TT > and <TT CLASS="parameter" ><I >errstr</I ></TT > 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.socket-set-blocking.html" ><B CLASS="function" >socket_set_blocking()</B ></A >. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN60605" ></A ><P ><B >Example 1. <B CLASS="function" >fsockopen()</B > Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fp = fsockopen ("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br>\n"; } else { fputs ($fp, "GET / HTTP/1.0\r\nHost: www.example.com\r\n\r\n"); while (!feof($fp)) { echo fgets ($fp,128); } fclose ($fp); } ?></PRE ></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="AEN60609" ></A ><P ><B >Example 2. Using UDP connection</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr); if (!$fp) { echo "ERROR: $errno - $errstr<br>\n"; } else { fwrite($fp,"\n"); echo fread($fp, 26); fclose($fp); } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warning</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 >Note: </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.socket-set-blocking.html" ><B CLASS="function" >socket_set_blocking()</B ></A >, <A HREF="function.socket-set-timeout.html" ><B CLASS="function" >socket_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.fputs.html" ><B CLASS="function" >fputs()</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" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.gethostbyaddr.html" ACCESSKEY="N" >Next</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" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >gethostbyaddr</TD ></TR ></TABLE ></DIV ></BODY ></HTML >