• PHP Video Tutorials

PHP - Function Fsockopen



Syntax

resource fsockopen ( 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 socket connections

Return Values

It Returns may be used together other file functions

Syslog variables

Sr.No Parameters & 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

Example

Try out following example

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

Above Example opens the connection

php_function_reference.htm
Advertisements