PHP pfsockopen() Function
The PHP pfsockopen() function opens persistent Internet or Unix domain socket connection. This function is almost identical to fsockopen() with the difference that the connection is not closed after the script finishes. It is the persistent version of fsockopen().
Syntax
pfsockopen(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.
Example: pfsockopen() example
The example below shows the usage of pfsockopen() 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>
❮ PHP Network Reference