Sophie

Sophie

distrib > Mageia > 7 > aarch64 > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 3636

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!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>Open Internet or Unix domain socket connection</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.dns-get-record.html">dns_get_record</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.gethostbyaddr.html">gethostbyaddr</a></div>
 <div class="up"><a href="ref.network.html">Network Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.fsockopen" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fsockopen</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">fsockopen</span> &mdash; <span class="dc-title">Open Internet or Unix domain socket connection</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fsockopen-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">resource</span> <span class="methodname"><strong>fsockopen</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$hostname</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$port</code><span class="initializer"> = -1</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$errno</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$errstr</code></span>
   [, <span class="methodparam"><span class="type">float</span> <code class="parameter">$timeout</code><span class="initializer"> = ini_get(&quot;default_socket_timeout&quot;)</span></span>
  ]]]] )</div>

  <p class="para rdfs-comment">
   Initiates a socket connection to the resource specified by
   <code class="parameter">hostname</code>.
  </p>
  <p class="para">
   PHP supports targets in the Internet and Unix domains as described in
   <a href="transports.html" class="xref">List of Supported Socket Transports</a>.  A list of supported transports can also be
   retrieved using <span class="function"><a href="function.stream-get-transports.html" class="function">stream_get_transports()</a></span>.
  </p>
  <p class="para">
   The socket will by default be opened in blocking mode.  You can
   switch it to non-blocking mode by using
   <span class="function"><a href="function.stream-set-blocking.html" class="function">stream_set_blocking()</a></span>.
  </p>
  <p class="para">
   The function <span class="function"><a href="function.stream-socket-client.html" class="function">stream_socket_client()</a></span> is similar but
   provides a richer set of options, including non-blocking connection and the
   ability to provide a stream context.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.fsockopen-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">hostname</code></dt>

     <dd>

      <p class="para">
       If OpenSSL support <a href="openssl.installation.html" class="link">is
       installed</a>, you may prefix the <code class="parameter">hostname</code>
       with either <em>ssl://</em> or <em>tls://</em> to
       use an SSL or TLS client connection over TCP/IP to connect to the
       remote host.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">port</code></dt>

     <dd>

      <p class="para">
       The port number. This can be omitted and skipped with
       <em>-1</em> for transports that do not use ports, such as
       <em>unix://</em>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">errno</code></dt>

     <dd>

      <p class="para">
       If provided, holds the system level error number that occurred in the
       system-level <em>connect()</em> call.
      </p>
      <p class="para">
       If the value returned in <code class="parameter">errno</code> is
       <em>0</em> and the function returned <strong><code>FALSE</code></strong>, it is an
       indication that the error occurred before the 
       <em>connect()</em> call. This is most likely due to a
       problem initializing the socket.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">errstr</code></dt>

     <dd>

      <p class="para">
       The error message as a string.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">timeout</code></dt>

     <dd>

      <p class="para">
       The connection timeout, in seconds.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        If you need to set a timeout for reading/writing data over the
        socket, use <span class="function"><a href="function.stream-set-timeout.html" class="function">stream_set_timeout()</a></span>, as the 
        <code class="parameter">timeout</code> parameter to
        <span class="function"><strong>fsockopen()</strong></span> only applies while connecting the
        socket.
       </p>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.fsockopen-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   <span class="function"><strong>fsockopen()</strong></span> returns a file pointer which may be used
   together with the other file functions (such as
   <span class="function"><a href="function.fgets.html" class="function">fgets()</a></span>, <span class="function"><a href="function.fgetss.html" class="function">fgetss()</a></span>,
   <span class="function"><a href="function.fwrite.html" class="function">fwrite()</a></span>, <span class="function"><a href="function.fclose.html" class="function">fclose()</a></span>, and
   <span class="function"><a href="function.feof.html" class="function">feof()</a></span>). If the call fails, it will return <strong><code>FALSE</code></strong>
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-function.fsockopen-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
   Throws <strong><code>E_WARNING</code></strong> if <code class="parameter">hostname</code> is
   not a valid domain.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.fsockopen-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-5609">
    <p><strong>Example #1 <span class="function"><strong>fsockopen()</strong></span> Example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fsockopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"www.example.com"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">80</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">30</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&nbsp;(</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">)&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"GET&nbsp;/&nbsp;HTTP/1.1\r\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Host:&nbsp;www.example.com\r\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Connection:&nbsp;Close\r\n\r\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">128</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5610">
    <p><strong>Example #2 Using UDP connection</strong></p>
    <div class="example-contents"><p>
     The example below shows how to retrieve the day and time from the UDP
     service &quot;daytime&quot; (port 13) in your own machine.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fsockopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"udp://127.0.0.1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">13</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"ERROR:&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">&nbsp;-&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">26</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.fsockopen-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Depending on the environment, the Unix domain or the optional
    connect timeout may not be available.
   </p>
  </p></blockquote>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    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 &quot;connectionless&quot; 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>
  </div>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">When specifying a numerical IPv6 address
(e.g. <em>fe80::1</em>), you must enclose the IP in square
brackets—for example, <em>tcp://[fe80::1]:80</em>.</span></p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.fsockopen-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.pfsockopen.html" class="function" rel="rdfs-seeAlso">pfsockopen()</a> - Open persistent Internet or Unix domain socket connection</span></li>
    <li class="member"><span class="function"><a href="function.stream-socket-client.html" class="function" rel="rdfs-seeAlso">stream_socket_client()</a> - Open Internet or Unix domain socket connection</span></li>
    <li class="member"><span class="function"><a href="function.stream-set-blocking.html" class="function" rel="rdfs-seeAlso">stream_set_blocking()</a> - Set blocking/non-blocking mode on a stream</span></li>
    <li class="member"><span class="function"><a href="function.stream-set-timeout.html" class="function" rel="rdfs-seeAlso">stream_set_timeout()</a> - Set timeout period on a stream</span></li>
    <li class="member"><span class="function"><a href="function.fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a> - Gets line from file pointer</span></li>
    <li class="member"><span class="function"><a href="function.fgetss.html" class="function" rel="rdfs-seeAlso">fgetss()</a> - Gets line from file pointer and strip HTML tags</span></li>
    <li class="member"><span class="function"><a href="function.fwrite.html" class="function" rel="rdfs-seeAlso">fwrite()</a> - Binary-safe file write</span></li>
    <li class="member"><span class="function"><a href="function.fclose.html" class="function" rel="rdfs-seeAlso">fclose()</a> - Closes an open file pointer</span></li>
    <li class="member"><span class="function"><a href="function.feof.html" class="function" rel="rdfs-seeAlso">feof()</a> - Tests for end-of-file on a file pointer</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">The <a href="ref.curl.html" class="link">Curl extension</a></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.dns-get-record.html">dns_get_record</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.gethostbyaddr.html">gethostbyaddr</a></div>
 <div class="up"><a href="ref.network.html">Network Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>