Current Location: Home> Latest Articles> Use stream_socket_client() and combine stream_context_get_options() to obtain socket configuration

Use stream_socket_client() and combine stream_context_get_options() to obtain socket configuration

M66 2025-05-28

In PHP, the stream_socket_client() function is used to open a network connection or Unix domain socket connection. It supports many options such as timeout settings, encryption, proxy, etc. These options are created by stream_context_create() and passed as context to stream_socket_client() .

This article will introduce how to use the stream_socket_client() function, combine stream_context_get_options() to obtain and view socket configuration.

Step 1: Create a Socket context

First, we need to create a context for stream_socket_client() . A context is a structure containing multiple configuration options that control the behavior of network connections. For example, we can set the timeout time for the connection, enable SSL encryption, etc.

 // Create a stream context
$options = [
    'http' => [
        'method' => 'GET',
        'header' => "User-Agent: PHP\r\n",
    ],
    'ssl' => [
        'verify_peer' => false,  // Not verifying peer certificates
        'verify_peer_name' => false,  // Not verifying peer certificates名称
    ]
];
$context = stream_context_create($options);

In the above code, we create an HTTP configuration and an SSL configuration, which is to allow the connection to not verify the peer certificate.

Step 2: Open the connection through stream_socket_client()

Next, we use stream_socket_client() to open a TCP connection. When connecting, we pass the context as a parameter.

 // Open with context TCP connect
$fp = stream_socket_client("tcp://m66.net:80", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);

if (!$fp) {
    echo "connect失败: $errno - $errstr\n";
    exit;
}

In this code, we connect to port 80 of m66.net . After the connection is successful, we can read and write data through $fp .

Step 3: Use stream_context_get_options() to view the configuration

Once the connection is established, we can use the stream_context_get_options() function to view the applied configuration options. This function returns an array containing all configuration options.

 // Get and display configuration options for the current stream
$options = stream_context_get_options($context);
echo '<pre>';
print_r($options);
echo '</pre>';

The above code will print the configuration options for the current stream. For example, if you set up HTTP and SSL configurations, the output will look like:

 Array
(
    [http] => Array
        (
            [method] => GET
            [header] => User-Agent: PHP
        )
    [ssl] => Array
        (
            [verify_peer] => 
            [verify_peer_name] => 
        )
)

Summarize

By combining stream_socket_client() and stream_context_get_options() , we can easily view and debug socket configuration. Whether it is setting the connection timeout, encryption options, or other custom configuration items, stream_context_get_options() can help us view the applied configuration.

Hope this article helps you better understand how to use streaming context in PHP and get related configuration.