<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Reads a maximum of length bytes from a socket</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.socket-listen.html">socket_listen</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.socket-recv.html">socket_recv</a></div> <div class="up"><a href="ref.sockets.html">Socket Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.socket-read" class="refentry"> <div class="refnamediv"> <h1 class="refname">socket_read</h1> <p class="verinfo">(PHP 4 >= 4.1.0, PHP 5)</p><p class="refpurpose"><span class="refname">socket_read</span> — <span class="dc-title">Reads a maximum of length bytes from a socket</span></p> </div> <div class="refsect1 description" id="refsect1-function.socket-read-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>socket_read</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$type</code><span class="initializer"> = PHP_BINARY_READ</span></span> ] )</div> <p class="para rdfs-comment"> The function <span class="function"><strong>socket_read()</strong></span> reads from the socket resource <em><code class="parameter">socket</code></em> created by the <span class="function"><a href="function.socket-create.html" class="function">socket_create()</a></span> or <span class="function"><a href="function.socket-accept.html" class="function">socket_accept()</a></span> functions. </p> </div> <div class="refsect1 parameters" id="refsect1-function.socket-read-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">socket</code></em></span> <dd> <p class="para"> A valid socket resource created with <span class="function"><a href="function.socket-create.html" class="function">socket_create()</a></span> or <span class="function"><a href="function.socket-accept.html" class="function">socket_accept()</a></span>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">length</code></em></span> <dd> <p class="para"> The maximum number of bytes read is specified by the <em><code class="parameter">length</code></em> parameter. Otherwise you can use <strong><code>\r</code></strong>, <strong><code>\n</code></strong>, or <strong><code>\0</code></strong> to end reading (depending on the <em><code class="parameter">type</code></em> parameter, see below). </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">type</code></em></span> <dd> <p class="para"> Optional <em><code class="parameter">type</code></em> parameter is a named constant: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <strong><code>PHP_BINARY_READ</code></strong> (Default) - use the system <em>recv()</em> function. Safe for reading binary data. </span> </li> <li class="listitem"> <span class="simpara"> <strong><code>PHP_NORMAL_READ</code></strong> - reading stops at <em>\n</em> or <em>\r</em>. </span> </li> </ul> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.socket-read-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <span class="function"><strong>socket_read()</strong></span> returns the data as a string on success, or <strong><code>FALSE</code></strong> on error (including if the remote host has closed the connection). The error code can be retrieved with <span class="function"><a href="function.socket-last-error.html" class="function">socket_last_error()</a></span>. This code may be passed to <span class="function"><a href="function.socket-strerror.html" class="function">socket_strerror()</a></span> to get a textual representation of the error. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> <span class="function"><strong>socket_read()</strong></span> returns a zero length string ("") when there is no more data to read. </p> </p></blockquote> </div> <div class="refsect1 changelog" id="refsect1-function.socket-read-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.1.0</td> <td> The default value for <em><code class="parameter">type</code></em> was changed from <strong><code>PHP_NORMAL_READ</code></strong> to <strong><code>PHP_BINARY_READ</code></strong> </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 seealso" id="refsect1-function.socket-read-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.socket-accept.html" class="function" rel="rdfs-seeAlso">socket_accept()</a> - Accepts a connection on a socket</span></li> <li class="member"> <span class="function"><a href="function.socket-bind.html" class="function" rel="rdfs-seeAlso">socket_bind()</a> - Binds a name to a socket</span></li> <li class="member"> <span class="function"><a href="function.socket-connect.html" class="function" rel="rdfs-seeAlso">socket_connect()</a> - Initiates a connection on a socket</span></li> <li class="member"> <span class="function"><a href="function.socket-listen.html" class="function" rel="rdfs-seeAlso">socket_listen()</a> - Listens for a connection on a socket</span></li> <li class="member"> <span class="function"><a href="function.socket-last-error.html" class="function" rel="rdfs-seeAlso">socket_last_error()</a> - Returns the last error on the socket</span></li> <li class="member"> <span class="function"><a href="function.socket-strerror.html" class="function" rel="rdfs-seeAlso">socket_strerror()</a> - Return a string describing a socket error</span></li> <li class="member"> <span class="function"><a href="function.socket-write.html" class="function" rel="rdfs-seeAlso">socket_write()</a> - Write to a socket</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.socket-listen.html">socket_listen</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.socket-recv.html">socket_recv</a></div> <div class="up"><a href="ref.sockets.html">Socket Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>