PHP | pfsockopen() Function

PHP pfsockopen() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP pfsockopen() Function.

The built-in pfsockopen() function opens a persistent internet or Unix domain socket connection.

Note: This function is almost identical to the fsockopen() function. The difference is that the connection is not closed after the script finishes. This built-in PHP function is the persistent version fsockopen() function.

Syntax

Following below is the syntax to use this function -

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


Parameter Details

Sr.NoParameters & Description
1

hostname

It contains the host name information.

2

port

It contains the port number.

3

errno

It provides the system level of error information.

4

errstr

It contains error message as a string

5

timeout

It contains the connection time out information.


Return Value

If connection is successful this function may return fgets(), fgetss(), fwrite(), fclose(), and feof() or else it returns false in case of failure.

Example

Try out the following example below -

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

?>


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we will study about the PHP setcookie() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.