Categories
PHP

How to open a socket connection

PHP can communicate with other servers using sockets. Sockets allow data exchange between processes on the same or different servers. PHP uses the fsockopen() function to open a socket connection to a web server.

Open a URL with file_get_contents()

When you use a file operation, you access a stream of data. In practice, it doesn’t really matter whether it’s a file on the local system, on a network share, or on a remote server connected via either HTTP, File Transfer Protocol (FTP), or any other supported protocol. Just provide the appropriate filename, and PHP takes care of the rest.

<?php 
 $html = file_get_contents('https://brainbell.com');
 echo $html;

The preceding code shows this: It opens the URL and prints its Hypertext Markup Language (HTML) code in the browser.

Note: For security reasons, this behavior can be turned off in php.ini by setting allow_url_fopen to Off.

Open a socket connection with fsockopen()

Let’s open a socket with fsockopen() to the web server, and if that works, then send a well-formed HTTP request. The fsockopen() function takes the following parameters:

  1. $hostname:
    The hostname or IP address of the remote server
  2. $port:
    The port number of the remote server.
    • -1 if the service does not use the port.
    • 80 for HTTP requests
    • 443 for HTTPS requests, for example, https://brainbell.com/.
  3. &$error_code:
    The error code if the connection fails.
  4. &$error_message:
    The error message if the connection fails.
  5. $timeout:
    The connection timeout, the maximum time in seconds to wait for the connection.

Here is an example of how to use fsockopen() function to connect to a web server and send a HTTP request:

<?php
 $fp = @fsockopen('brainbell.com', 80, $errno, $errstr, 30);

 if ($fp) {
  // Send Request Headers
  $request = "GET / HTTP/1.1\r\n";
  $request .= "Host: brainbell.com\r\n";
  $request .= "Connection: Close\r\n\r\n";
  fwrite($fp, $request);

  // Print Response
  while (!feof($fp)) {
    echo fgets($fp, 1024);
  }
  fclose($fp);
  } else {
    echo "Error: $errstr (#$errno)";
  }

The function returns a resource handle that can be used with other functions, such as fwrite, fread, fclose, etc. If the connection fails, the function returns false and sets the $errno and $errstr variables.

The output will same as with file_get_contents() function with one difference: The fsockopen() function also returns all HTTP headers sent by the server.

Accessing HTTPS URL with fsockopen

When it is required to work with HTTP Secure (HTTPS) resources (websites secured with SSLSecure Sockets Layer) use an ssl:// URL, see code:

When it is required to work with HTTP Secure (HTTPS) resources (websites secured with SSLSecure Sockets Layer), the two slight modifications are required:

  1. Write ssl:// in the hostname
  2. Use 443 port
<?php
 $fp = @fsockopen('ssl://brainbell.com', 443, $errno, $errstr, 30);

Communicating with Servers: