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.
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
Parameter
Description
hostname
Required. Hostname, IP, or transport://target such as ssl://example.com or a Unix socket path.
port
Optional. TCP/UDP port number. Use -1 when the hostname already includes a Unix socket path.
error_code
Optional. Variable passed by reference that receives the system error number on failure.
error_message
Optional. Variable passed by reference that receives the system error string on failure.
timeout
Optional. Connection timeout in seconds. null uses the default socket timeout.
Return Value
Return Value
Description
resource
A stream resource you can use with fwrite(), fgets(), feof(), and fclose().
false
The 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.
Port 1 is almost never open on a beginner machine.
The @ operator hides the PHP warning so the demo stays tidy.
false means the socket was not created.
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;
Output
fsockopen is non-persistent
pfsockopen keeps the socket for reuse when the runtime allows it
shared timeout argument: 5
shared error args: errno + errstr
How It Works
Both functions take host, port, error refs, and timeout.
fsockopen() closes when the script ends.
pfsockopen() may keep the connection for later requests in the same process.
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.