English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP pfsockopen() Function Usage and Example

PHP HTTP Reference Manual

The pfsockopen() function opens a persistent network connection or Unix domain socket connection.

Syntax

resource pfsockopen(string $hostname[, int $port = -1 [, int &$errno[, string &$errstr[, float $timeout = ini_get("default_socket_timeout")]]])

Definition and Usage

It is used to open Internet or Unix domain sockets

Return Value

If the connection is successful, it may return fgets(), fgetss(), fwrite(), fclose(), and feof(), otherwise it will return False in the case of failure.

 This function works exactly the same as fsockopen(), the difference is that the connection will not be closed after the script execution is completed. It can be said that it is the long connection version of fsockopen().

Parameter

Serial NumberParameters and Description
1

hostname

It contains hostname information.

2

port

It contains the port number.

3

errno

It provides system-level error information.

4

errstr

It contains error messages as strings.

5

timeout

It contains connection timeout information.

Online Example

Try the following example

<?php
   $open = fsockopen("www.w"3codebox.com", 80, $errno, $errstr, 30);
   
   if (!$open) {
      echo "$errstr ($errno"
      \n"
   } else {
   $out = "GET" / HTTP/1.1\r\n"
   $out .= "Host: www.w"3codebox.com\r\n";
   $out .= "Connection: Close\r\n\r\n";
   
   fwrite($open, $out);
   
   while (!feof($open)) {
      echo fgets($open, 128);
   }
   
   fclose($open);
?>

PHP HTTP Reference Manual