<!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>Socket context option listing</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="context.html">Context options and parameters</a></div> <div class="next" style="text-align: right; float: right;"><a href="context.http.html">HTTP context options</a></div> <div class="up"><a href="context.html">Context options and parameters</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="context.socket" class="refentry"> <div class="refnamediv"> <h1 class="refname">Socket context options</h1> <p class="refpurpose"><span class="refname">Socket context options</span> — <span class="dc-title">Socket context option listing</span></p> </div> <div class="refsect1 description" id="refsect1-context.socket-description"> <h3 class="title">Description</h3> <p class="para"> Socket context options are available for all wrappers that work over sockets, like <em>tcp</em>, <em>http</em> and <em>ftp</em>. </p> </div> <div class="refsect1 options" id="refsect1-context.socket-options"> <h3 class="title">Options</h3> <p class="para"> <dl> <dt id="context.socket.bindto"> <span class="term"><em><code class="parameter">bindto</code></em></span> <dd> <p class="para"> Used to specify the IP address (either IPv4 or IPv6) and/or the port number that PHP will use to access the network. The syntax is <em>ip:port</em> for IPv4 addresses, and <em>[ip]:port</em> for IPv6 addresses. Setting the IP or the port to <em>0</em> will let the system choose the IP and/or port. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> As FTP creates two socket connections during normal operation, the port number cannot be specified using this option. </p> </p></blockquote> </dd> </dt> <dt id="context.socket.backlog"> <span class="term"><em><code class="parameter">backlog</code></em></span> <dd> <p class="para"> Used to limit the number of outstanding connections in the socket's listen queue. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> This is only applicable to <span class="function"><a href="function.stream-socket-server.html" class="function">stream_socket_server()</a></span>. </p> </p></blockquote> </dd> </dt> </dl> </p> </div> <div class="refsect1 changelog" id="refsect1-context.socket-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>5.3.3</td> <td> Added <em>backlog</em>. </td> </tr> <tr> <td>5.1.0</td> <td> Added <em>bindto</em>. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-context.socket-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="context.socket.example-bindto"> <p><strong>Example #1 Basic <em><code class="parameter">bindto</code></em> usage example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// connect to the internet using the '192.168.0.100' IP<br /></span><span style="color: #0000BB">$opts </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">'socket' </span><span style="color: #007700">=> array(<br /> </span><span style="color: #DD0000">'bindto' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'192.168.0.100:0'</span><span style="color: #007700">,<br /> ),<br />);<br /><br /><br /></span><span style="color: #FF8000">// connect to the internet using the '192.168.0.100' IP and port '7000'<br /></span><span style="color: #0000BB">$opts </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">'socket' </span><span style="color: #007700">=> array(<br /> </span><span style="color: #DD0000">'bindto' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'192.168.0.100:7000'</span><span style="color: #007700">,<br /> ),<br />);<br /><br /><br /></span><span style="color: #FF8000">// connect to the internet using the '2001:db8::1' IPv6 address<br />// and port '7000'<br /></span><span style="color: #0000BB">$opts </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">'socket' </span><span style="color: #007700">=> array(<br /> </span><span style="color: #DD0000">'bindto' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'[2001:db8::1]:7000'</span><span style="color: #007700">,<br /> ),<br />);<br /><br /><br /></span><span style="color: #FF8000">// connect to the internet using port '7000'<br /></span><span style="color: #0000BB">$opts </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">'socket' </span><span style="color: #007700">=> array(<br /> </span><span style="color: #DD0000">'bindto' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'0:7000'</span><span style="color: #007700">,<br /> ),<br />);<br /><br /><br /></span><span style="color: #FF8000">// create the context...<br /></span><span style="color: #0000BB">$context </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// ...and use it to fetch the data<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com'</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="context.html">Context options and parameters</a></div> <div class="next" style="text-align: right; float: right;"><a href="context.http.html">HTTP context options</a></div> <div class="up"><a href="context.html">Context options and parameters</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>