<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.network.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.fsockopen.php',
    1 => 'fsockopen',
    2 => 'Open Internet or Unix domain socket connection',
  ),
  'up' => 
  array (
    0 => 'ref.network.php',
    1 => 'Функції для мережі',
  ),
  'prev' => 
  array (
    0 => 'function.dns-get-record.php',
    1 => 'dns_get_record',
  ),
  'next' => 
  array (
    0 => 'function.gethostbyaddr.php',
    1 => 'gethostbyaddr',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/network/functions/fsockopen.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.fsockopen" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fsockopen</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</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">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fsockopen</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$hostname</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$port</code><span class="initializer"> = -1</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter reference">&$error_code</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter reference">&$error_message</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.float.php" class="type float">float</a></span></span> <code class="parameter">$timeout</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span><br>): <span class="type"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></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.php" class="xref">Перелік підтримуваних транспортних протоколів у сокетах</a>.  A list of supported transports can also be
   retrieved using <span class="function"><a href="function.stream-get-transports.php" 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.php" class="function">stream_set_blocking()</a></span>.
  </p>
  <p class="para">
   The function <span class="function"><a href="function.stream-socket-client.php" 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">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">hostname</code></dt>
     <dd>
      <p class="para">
       If OpenSSL support <a href="openssl.installation.php" class="link">is
       installed</a>, you may prefix the <code class="parameter">hostname</code>
       with either <code class="literal">ssl://</code> or <code class="literal">tls://</code> 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
       <code class="literal">-1</code> for transports that do not use ports, such as
       <code class="literal">unix://</code>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">error_code</code></dt>
     <dd>
      <p class="para">
       If provided, holds the system level error number that occurred in the
       system-level <code class="literal">connect()</code> call.
      </p>
      <p class="para">
       If the value returned in <code class="parameter">error_code</code> is
       <code class="literal">0</code> and the function returned <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, it is an
       indication that the error occurred before the 
       <code class="literal">connect()</code> call. This is most likely due to a
       problem initializing the socket.
      </p>
     </dd>
    
    
     <dt><code class="parameter">error_message</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. When <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>, the
       <a href="filesystem.configuration.php#ini.default-socket-timeout" class="link">default_socket_timeout</a> <var class="filename">php.ini</var> setting is used.
      </p>
      <blockquote class="note"><p><strong class="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.php" 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">Значення, що повертаються</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.php" class="function">fgets()</a></span>, <span class="function"><a href="function.fgetss.php" class="function">fgetss()</a></span>,
   <span class="function"><a href="function.fwrite.php" class="function">fwrite()</a></span>, <span class="function"><a href="function.fclose.php" class="function">fclose()</a></span>, and
   <span class="function"><a href="function.feof.php" class="function">feof()</a></span>). If the call fails, it will return <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-function.fsockopen-errors">
  <h3 class="title">Помилки/виключення</h3>
  <p class="para">
   Throws <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> if <code class="parameter">hostname</code> is
   not a valid domain.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.fsockopen-changelog">
  <h3 class="title">Журнал змін</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Версія</th>
      <th>Опис</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">timeout</code> is nullable now.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.fsockopen-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #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 </span><span style="color: #007700">= </span><span style="color: #0000BB">fsockopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"www.example.com"</span><span style="color: #007700">, </span><span style="color: #0000BB">80</span><span style="color: #007700">, </span><span style="color: #0000BB">$errno</span><span style="color: #007700">, </span><span style="color: #0000BB">$errstr</span><span style="color: #007700">, </span><span style="color: #0000BB">30</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000"> (</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">)&lt;br /&gt;\n"</span><span style="color: #007700">;<br />} else {<br />    </span><span style="color: #0000BB">$out </span><span style="color: #007700">= </span><span style="color: #DD0000">"GET / HTTP/1.1\r\n"</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">$out </span><span style="color: #007700">.= </span><span style="color: #DD0000">"Host: www.example.com\r\n"</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">$out </span><span style="color: #007700">.= </span><span style="color: #DD0000">"Connection: Close\r\n\r\n"</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">$out</span><span style="color: #007700">);<br />    while (!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)) {<br />        echo </span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">128</span><span style="color: #007700">);<br />    }<br />    </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-2">
    <p><strong>Приклад #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 </span><span style="color: #007700">= </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">, </span><span style="color: #0000BB">13</span><span style="color: #007700">, </span><span style="color: #0000BB">$errno</span><span style="color: #007700">, </span><span style="color: #0000BB">$errstr</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"ERROR: </span><span style="color: #0000BB">$errno</span><span style="color: #DD0000"> - </span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&lt;br /&gt;\n"</span><span style="color: #007700">;<br />} else {<br />    </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br />    echo </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">26</span><span style="color: #007700">);<br />    </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">Примітки</h3>
  <blockquote class="note"><p><strong class="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">Увага</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">Зауваження</strong>: <span class="simpara">Вказуючи числову
IPv6-адресу (напр. <code class="literal">fe80::1</code>), необхідно взяти IP-адресу в
квадратні дужки, наприклад,
<code class="literal">tcp://[fe80::1]:80</code>.</span></p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.fsockopen-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.pfsockopen.php" class="function" rel="rdfs-seeAlso">pfsockopen()</a> - Open persistent Internet or Unix domain socket connection</span></li>
    <li><span class="function"><a href="function.stream-socket-client.php" class="function" rel="rdfs-seeAlso">stream_socket_client()</a> - Open Internet or Unix domain socket connection</span></li>
    <li><span class="function"><a href="function.stream-set-blocking.php" class="function" rel="rdfs-seeAlso">stream_set_blocking()</a> - Set blocking/non-blocking mode on a stream</span></li>
    <li><span class="function"><a href="function.stream-set-timeout.php" class="function" rel="rdfs-seeAlso">stream_set_timeout()</a> - Set timeout period on a stream</span></li>
    <li><span class="function"><a href="function.fgets.php" class="function" rel="rdfs-seeAlso">fgets()</a> - Gets line from file pointer</span></li>
    <li><span class="function"><a href="function.fgetss.php" class="function" rel="rdfs-seeAlso">fgetss()</a> - Gets line from file pointer and strip HTML tags</span></li>
    <li><span class="function"><a href="function.fwrite.php" class="function" rel="rdfs-seeAlso">fwrite()</a> - Binary-safe file write</span></li>
    <li><span class="function"><a href="function.fclose.php" class="function" rel="rdfs-seeAlso">fclose()</a> - Closes an open file pointer</span></li>
    <li><span class="function"><a href="function.feof.php" class="function" rel="rdfs-seeAlso">feof()</a> - Tests for end-of-file on a file pointer</span></li>
    <li><span class="function"><a href="function.socket-connect.php" class="function" rel="rdfs-seeAlso">socket_connect()</a> - Initiates a connection on a socket</span></li>
    <li>The <a href="ref.curl.php" class="link">Curl extension</a></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>