PHP Function Reference

PHP fsockopen() Function



The PHP fsockopen() function opens Internet or Unix domain socket connection.

By default, the socket is opened in blocking mode. It can be switched to non-blocking mode by using stream_set_blocking().

The function stream_socket_client() is similar but provides a richer set of options, including non-blocking connection and the ability to provide a stream context.

Syntax

fsockopen(hostname, port, error_code, error_message, timeout)

Parameters

hostname Required. Specify a hostname (like "www.alphacodingskills.com"). If OpenSSL support is installed, the hostname can be prefixed with either ssl:// or tls:// to use an SSL or TLS client connection over TCP/IP to connect to the remote host.
port Optional. Specify the port number. This can be omitted and skipped with -1 for transports that do not use ports, like unix://.
error_code Optional. Specify the system level error number.
error_message Optional. Specify the error message as a string.
timeout Optional. Specify the connection timeout, in seconds. When null, the default_socket_timeout of php.ini setting is used.

Return Value

Returns a file pointer which can be used together with the other file functions, such as fgets(), fgetss(), fwrite(), fclose(), and feof(), or false on failure.

Exceptions

Throws E_WARNING if hostname is not a valid domain.

Example: fsockopen() example

The example below shows the usage of fsockopen() function.

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 20);
if (!$fp) {
  echo "$errstr ($errno)<br>\n";
} else {
  $out = "GET / HTTP/1.1\r\n";
  $out .= "Host: www.example.com\r\n";
  $out .= "Connection: Close\r\n\r\n";
  fwrite($fp, $out);
  while (!feof($fp)) {
    echo fgets($fp, 128);
  }
  fclose($fp);
}
?>

The output of the above code will be similar to:

HTTP/1.1 200 OK
Age: 491454
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Sun, 31 Oct 2021 08:32:38 GMT
Etag: "3147526947+ident"
Expires: Sun, 07 Nov 2021 08:32:38 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (dna/63AA)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256
Connection: close

<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", 
                     "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        
    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>    
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

Example: using UDP connection

In the example below, the fsockopen() function is used with UDP connection.

<?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);
}
?>

❮ PHP Network Reference