We now have a youtube channel. Subscribe!

PHP | fsockopen() Function

PHP fsockopen() 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 fsockopen() Function.

The built-in fsockopen() function is used for opening the internet or Unix domain socket connections.

Syntax

Following below is the syntax to use this function -

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


Parameter Details

Sr.NoParameters & Description
1

hostname

ssl:// or tls:// are works over on TCP/IP to connect to the remote host.

2

port

The port number. This can be omitted and skipped with -1 for transports that do not use ports, such as unix://.

3

errno

It Provides the system level error number

4

errstr

The Error message as a string

5

timeout

The Connection time Out


Return Value

This built-in function returns a file pointer which may be used together with other file functions. If the call fails, it returns FALSE.

Example

Try out the following example below -

<?php
   $connection = fsockopen("www.webdesigntutorialz.com", 80, $errno, $errstr, 30);
   
   if (!$connection) {
      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($connection, $out);
      
      while (!feof($connection)) {
         echo fgets($connection, 128);
      }
      fclose($connection);
   }
?>


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 gethostbyaddr() 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.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain