PHP pfsockopen function tutorial shows how to create persistent network connections in PHP. Learn pfsockopen with practical examples for HTTP, SMTP, and custom protocols.
last modified April 4, 2025
The PHP pfsockopen function opens a persistent Internet or Unix domain socket connection. It’s similar to fsockopen but maintains the connection between requests.
pfsockopen establishes a persistent socket connection to a specified host and port. The connection remains open across multiple script executions.
Syntax: pfsockopen(string $hostname, int $port = -1, int &$errno = null, string &$errstr = null, float $timeout = ini_get(“default_socket_timeout”)). Returns a file pointer on success, false on failure.
This example demonstrates making a simple HTTP GET request using pfsockopen.
http_request.php
<?php
declare(strict_types=1);
$fp = pfsockopen(“www.example.com”, 80, $errno, $errstr, 30);
if (!$fp) { echo “Error: $errstr ($errno)”; } 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);
}
This connects to example.com on port 80 and sends a basic HTTP GET request. The response is read line by line and output. The connection is persistent.
This example shows how to send an email through SMTP using pfsockopen.
smtp_email.php
<?php
declare(strict_types=1);
$smtp = pfsockopen(“mail.example.com”, 25, $errno, $errstr, 30);
if (!$smtp) { die(“Error: $errstr ($errno)”); }
function smtp_command($socket, $cmd) { fwrite($socket, $cmd . “\r\n”); return fgets($socket, 512); }
echo smtp_command($smtp, “EHLO localhost”); echo smtp_command($smtp, “MAIL FROM: <sender@example.com>”); echo smtp_command($smtp, “RCPT TO: <recipient@example.com>”); echo smtp_command($smtp, “DATA”); echo smtp_command($smtp, “Subject: Test\r\n\r\nHello World\r\n.”); echo smtp_command($smtp, “QUIT”);
fclose($smtp);
This connects to an SMTP server and sends a basic email. The connection persists between commands. Each SMTP command gets a response from the server.
This example demonstrates communication with a custom TCP server.
custom_tcp.php
<?php
declare(strict_types=1);
$socket = pfsockopen(“tcp://127.0.0.1”, 9000, $errno, $errstr, 30);
if (!$socket) { die(“Error: $errstr ($errno)”); }
fwrite($socket, “PING\r\n”); $response = fgets($socket, 1024);
echo “Server response: $response”;
fwrite($socket, “QUIT\r\n”); fclose($socket);
This connects to a local TCP server on port 9000. It sends a PING command and reads the response. The connection remains open for subsequent requests.
This example shows how to establish a secure HTTPS connection.
https_connection.php
<?php
declare(strict_types=1);
$context = stream_context_create([ ‘ssl’ => [ ‘verify_peer’ => false, ‘verify_peer_name’ => false ] ]);
$fp = pfsockopen(‘ssl://www.example.com’, 443, $errno, $errstr, 30, $context);
if (!$fp) { die(“Error: $errstr ($errno)”); }
$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);
This establishes a secure HTTPS connection using SSL. The stream context allows configuration of SSL parameters. The connection persists for reuse.
This example demonstrates communication with a Unix domain socket.
unix_socket.php
<?php
declare(strict_types=1);
$socket = pfsockopen(“unix:///var/run/myservice.sock”, 0, $errno, $errstr, 30);
if (!$socket) { die(“Error: $errstr ($errno)”); }
fwrite($socket, “STATUS\r\n”); $response = fgets($socket, 1024);
echo “Service status: $response”;
fclose($socket);
This connects to a Unix domain socket at /var/run/myservice.sock. It sends a STATUS command and reads the response. The connection remains persistent.
Error Handling: Always check for connection errors
Timeouts: Set appropriate timeout values
Cleanup: Close connections when done
Security: Validate all input/output
Performance: Reuse connections when possible
This tutorial covered the PHP pfsockopen function with practical examples for various network communication scenarios using persistent connections.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.