
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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.