Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Network socket_get_status() Function



The PHP Network socket_get_status() function provides an easy way to acquire information about a socket connection. It is effectively an alias for another function called stream_get_meta_data. This suggests that it serves the same purpose as stream_get_meta_data, namely to provide information about the status of a socket or stream. It provides useful information, such as whether the connection is active or has timed out. You may also use it to determine whether any data is ready to be read from the connection.

This function is compatible with PHP versions 4, 5, 7, and 8, therefore it may be used in the majority of PHP projects. It is simple to use and effective for managing network connections in PHP applications. Since it is an alias, you can use stream_get_meta_data instead for the same results. This makes it a flexible option for developers handling sockets or streams in PHP.

Syntax

Below is the syntax of the PHP Network socket_get_status() function −

array socket_get_status ( resource $stream );

Parameters

This function accepts $stream parameter which is a valid stream resource created by functions like fsockopen(), stream_socket_client(), or equivalent. This stream shows the connection you want to verify.

Return Value

The socket_get_status() function returns an associative array containing metadata about the stream.

Key details include −

  • timed_out: (bool) True if the stream has timed out.

  • blocked: (bool) True if the stream is in blocking mode.

  • eof: (bool) True if the stream has reached the end-of-file.

  • unread_bytes: (int) Number of bytes available to read from the stream.

  • stream_type: (string) The type of the stream (e.g., "tcp_socket").

  • wrapper_type: (string) The wrapper type used (e.g., "http", "tcp").

  • wrapper_data: (mixed) Any additional information about the wrapper.

  • mode: (string) The mode in which the stream was opened (e.g., "r", "w").

  • seekable: (bool) True if the stream is seekable.

PHP Version

First introduced in core PHP 4, the socket_get_status() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP Network socket_get_status() function to check stream metadata. So the program below opens a socket connection to a website and uses this function to fetch basic metadata about the connection.

<?php
   // Open a connection on port 80 
   $stream = fsockopen("tutorialspoint.com", 80);

   // Get stream metadata using socket_get_status
   $status = socket_get_status($stream);

   // Print metadata
   print_r($status);

   // Close the stream
   fclose($stream);
?>

Output

Here is the outcome of the following code −

Array
(
   [timed_out] => 
   [blocked] => 1
   [eof] => 
   [stream_type] => tcp_socket/ssl
   [mode] => r+
   [unread_bytes] => 0
   [seekable] => 
   [uri] => tutorialspoint.com:80
)

Example 2

In the below PHP code we will use the socket_get_status() function and check for timed out connection. So this program sets a timeout on a socket connection and uses the function to verify if the connection has timed out.

<?php
   // Open a connection with a timeout
   $stream = fsockopen("tutorialspoint.com", 80, $errno, $errstr, 2);

   // Simulate a timeout by waiting
   sleep(5); 

   // Get metadata 
   $status = socket_get_status($stream);

   if ($status['timed_out']) {
      echo "Connection has timed out.";
   } else {
      echo "Connection is active.";
   }

   // Close the stream
   fclose($stream);
?> 

Output

This will generate the below output −

Connection is active.

Example 3

Now the below code shows how to set a stream to non-blocking mode with the help of the socket_get_status() function and check if data is ready to be read.

<?php
   // Open a socket connection
   $stream = fsockopen("tutorialspoint.com", 80);

   // Set the stream to non-blocking mode
   stream_set_blocking($stream, false);

   // Fetch metadata 
   $status = socket_get_status($stream);

   if ($status['unread_bytes'] > 0) {
      echo "Data is ready to be read.";
   } else {
      echo "No data available yet.";
   }

   // Close the stream
   fclose($stream);
?> 

Output

This will create the below output −

No data available yet.
php_function_reference.htm
Advertisements