PHP pfsockopen() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, pfsockopen() opens a network or Unix-domain socket the same way fsockopen() does, but keeps the connection persistent across requests in environments that support it.

Persistent means PHP may reuse the same underlying connection for later scripts in the same process instead of closing it when the first script ends. For beginners, treat it like fsockopen() with persistence: check for false, read and write with stream functions, and understand that not every host keeps sockets alive forever.

Syntax

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

PHP
$fp = pfsockopen('example.com', 80, $errno, $errstr, 10);
CallWhat it does
pfsockopen('example.com', 80)Opens a persistent TCP connection to port 80.
pfsockopen('ssl://example.com', 443, $errno, $errstr, 15)Opens a persistent TLS socket and captures error details.
pfsockopen('127.0.0.1', 25, $errno, $errstr, 5)Tries a local SMTP port with a 5 second timeout.

Parameters match fsockopen(): host, port, optional error code and message by reference, and optional timeout. Prefix ssl:// or tls:// when you need encryption. On failure the function returns false and fills $error_code / $error_message.

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

Parameters

ParameterDescription
hostnameRequired. Hostname, IP, or transport://target such as ssl://example.com or a Unix socket path.
portOptional. TCP/UDP port number. Use -1 when the hostname already includes a Unix socket path.
error_codeOptional. Variable passed by reference that receives the system error number on failure.
error_messageOptional. Variable passed by reference that receives the system error string on failure.
timeoutOptional. Connection timeout in seconds. null uses the default socket timeout.

Return Value

Return ValueDescription
resourceA stream resource you can use with fwrite(), fgets(), feof(), and fclose().
falseThe connection could not be opened.

PHP Examples

Example 1 — Open a Persistent HTTP Socket

Connect to a demo host on port 80, send a tiny request, and print one status line. Output below is a typical successful sample.

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

if ($fp === false) {
    echo "Connect failed: $errno $errstr", PHP_EOL;
    exit;
}

fwrite($fp, "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: Close\r\n\r\n");
$status = fgets($fp);
echo trim($status), PHP_EOL;
fclose($fp);

How It Works

  1. pfsockopen() tries a persistent TCP connection to example. com:80.
  2. On failure we print the error code and message.
  3. fwrite() sends a minimal HTTP request.
  4. fgets() reads the status line from the response.

Example 2 — Handle a Connection Failure

Point at a closed local port so the call fails, then show errno and errstr.

PHP
<?php
$fp = @pfsockopen('127.0.0.1', 1, $errno, $errstr, 1);

if ($fp === false) {
    echo 'failed', PHP_EOL;
    echo 'code: ', $errno, PHP_EOL;
    echo 'message: ', $errstr, PHP_EOL;
}

How It Works

  1. Port 1 is almost never open on a beginner machine.
  2. The @ operator hides the PHP warning so the demo stays tidy.
  3. false means the socket was not created.
  4. The by-reference arguments explain why the connect failed.

Example 3 — Compare with fsockopen()

Show that both functions share the same call shape; only persistence differs.

PHP
<?php
echo 'fsockopen is non-persistent', PHP_EOL;
echo 'pfsockopen keeps the socket for reuse when the runtime allows it', PHP_EOL;

$timeout = 5;
echo 'shared timeout argument: ', $timeout, PHP_EOL;
echo 'shared error args: errno + errstr', PHP_EOL;

How It Works

  1. Both functions take host, port, error refs, and timeout.
  2. fsockopen() closes when the script ends.
  3. pfsockopen() may keep the connection for later requests in the same process.
  4. Choose based on whether reuse is useful in your hosting model.

Frequently Asked Questions

It opens an Internet or Unix-domain socket like fsockopen(), but requests a persistent connection that PHP may reuse.
The call signature and stream usage are the same. The difference is persistence: pfsockopen() is the persistent variant.
No. It depends on how PHP runs (for example long-lived workers vs short CGI processes). Treat reuse as a possible optimization, not a promise.
Use normal stream functions such as fwrite(), fgets(), stream_get_contents(), and fclose() on the returned resource.
Check for false and read $error_code and $error_message. Also set a timeout so a bad host does not hang your script.

Next: request_parse_body()

request_parse_body() reads the raw HTTP request body and parses application/x-www-form-url.

request_parse_body() 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