PHP fsockopen() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, fsockopen() opens a socket connection to a host and port (or to a Unix-domain transport) and returns a stream resource you can read and write with file functions. On failure it returns false.

Pass optional reference arguments to capture the error code and error message when the connect fails. A timeout limits how long the connection attempt may wait. For richer options, stream_socket_client() is often preferred in modern code.

Syntax

Call fsockopen() with the values you need. Start with this line.

PHP
$fp = fsockopen("example.com", 80, $errno, $errstr, 10);
CallWhat it does
fsockopen("example.com", 80)Opens a TCP connection to port 80 and returns a stream or false.
fsockopen("example.com", 80, $errno, $errstr)Same, but fills $errno and $errstr on failure.
fsockopen("ssl://example.com", 443, $errno, $errstr, 15)Opens a TLS/SSL connection with a 15 second connect timeout.

Use fclose() when finished. The timeout only applies while connecting; use stream_set_timeout() for read/write timeouts. Prefix the host with ssl:// or tls:// when OpenSSL support is available.

Library declarations
PHP
fsockopen(string $hostname, int $port = -1, int &$error_code = null, string &$error_message = null, ?float $timeout = null): resource|false

Parameters

ParameterDescription
hostnameRequired. The host to connect to. May include a transport prefix such as ssl:// or tls://.
portOptional. TCP port number. Use -1 for transports that do not use ports (for example unix://).
error_codeOptional. Passed by reference; receives the system error number if the connect fails.
error_messageOptional. Passed by reference; receives a human-readable error string on failure.
timeoutOptional. Connection timeout in seconds. When null, default_socket_timeout from php.ini is used.

Return Value

Return ValueDescription
resourceA stream resource you can use with fwrite(), fgets(), fclose() and similar functions.
falseReturned when the connection cannot be opened.

PHP Examples

Example 1 — Open a Socket and Send a Tiny HTTP Request

Connect to port 80, write a simple request, read one status line, then close. Output is a typical sample response line.

PHP
<?php
$fp = fsockopen("example.com", 80, $errno, $errstr, 10);

if (!$fp) {
    echo "Connect failed: $errno $errstr", PHP_EOL;
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    echo fgets($fp);
    fclose($fp);
}

How It Works

  1. fsockopen() tries to connect with a 10 second timeout.
  2. On failure we print $errno and $errstr.
  3. On success we write a minimal HTTP request with fwrite().
  4. fgets() reads the first response line, then fclose() closes the stream.

Example 2 — Handle a Failed Connect

Show how the error reference arguments look when the host cannot be reached. Sample values are fixed for the lesson.

PHP
<?php
$fp = fsockopen("invalid.example.invalid", 80, $errno, $errstr, 2);

if ($fp === false) {
    echo "errno=", $errno, PHP_EOL;
    echo "errstr=", $errstr, PHP_EOL;
} else {
    fclose($fp);
    echo "unexpected success", PHP_EOL;
}

How It Works

  1. We deliberately use a hostname that will not resolve.
  2. fsockopen() returns false.
  3. $errno and $errstr explain the failure.
  4. An errno of 0 often means the problem happened before connect(), such as DNS failure.

Example 3 — Check That a Port Accepts Connections

Use a short timeout to test whether a TCP port answers. Result text is a typical demo.

PHP
<?php
$host = "example.com";
$port = 80;

$fp = @fsockopen($host, $port, $errno, $errstr, 3);

if ($fp) {
    echo "Port $port is open", PHP_EOL;
    fclose($fp);
} else {
    echo "Port $port is closed or blocked ($errstr)", PHP_EOL;
}

How It Works

  1. A 3 second timeout keeps the check snappy.
  2. A usable stream means the TCP handshake succeeded.
  3. Always fclose() successful sockets.
  4. "Port 80 is open" is sample success text for the tutorial.

Frequently Asked Questions

fsockopen() opens a network (or Unix) socket and returns a stream resource for reading and writing, or false on failure.
Pass variables for $error_code and $error_message by reference. When the call returns false, those variables hold the system error number and message.
No. The timeout only limits the connect attempt. Use stream_set_timeout() to limit later reads and writes.
pfsockopen() opens a persistent socket that can survive between requests in some SAPIs. fsockopen() opens a normal non-persistent connection.
Yes, if OpenSSL support is enabled. Prefix the host with ssl:// or tls:// and connect to port 443.

Next: gethostbyaddr()

gethostbyaddr() performs a reverse DNS lookup: you give it an IP address and it returns th.

gethostbyaddr() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful