Current Location: Home> Latest Articles> The Relationship Between stream_notification_callback and PHP Stream Contexts, Understanding Its Role and Impact

The Relationship Between stream_notification_callback and PHP Stream Contexts, Understanding Its Role and Impact

gitbox 2025-08-14

What Is a Stream Context?

In PHP, a stream context is used to configure the properties and behavior of streams. With a stream context, developers can define different types of stream operations, specify read and write modes, and control how resources are opened. Essentially, a stream context is an array or resource containing stream-related options that PHP uses to configure various aspects of stream operations.

Common stream context settings include:

  • stream.wrapper_data: Used to specify the configuration for a stream wrapper.

  • http: Used to set options for HTTP streams, such as request headers and proxy settings.

  • ssl: Used to configure SSL connections, including certificates and passwords.

For example, creating a stream context can be done with the following code:

<span><span><span class="hljs-variable">$options</span></span><span> = </span><span><span class="hljs-keyword">array</span></span><span>(
    </span><span><span class="hljs-string">"http"</span></span><span> => </span><span><span class="hljs-keyword">array</span></span><span>(
        </span><span><span class="hljs-string">"method"</span></span><span>  => </span><span><span class="hljs-string">"GET"</span></span><span>,
        </span><span><span class="hljs-string">"header"</span></span><span>  => </span><span><span class="hljs-string">"Accept-language: en\r\n"</span></span><span>
    )
);
</span><span><span class="hljs-variable">$context</span></span><span> = </span><span><span class="hljs-title function_ invoke__">stream_context_create</span></span><span>(</span><span><span class="hljs-variable">$options</span></span><span>);
</span></span>

A stream context is created via the stream_context_create() function and can be used in file or network stream operations.


The Role of stream_notification_callback

stream_notification_callback is a callback function in PHP related to streams. It is used to receive and handle notifications during stream operations. When a stream operation encounters specific events (such as a failed file open or interrupted network connection), PHP can notify the developer through the callback function. Its basic syntax is as follows:

<span><span><span class="hljs-title function_ invoke__">stream_context_set_option</span></span><span>(</span><span><span class="hljs-variable">$context</span></span><span>, </span><span><span class="hljs-string">"notification"</span></span><span>, </span><span><span class="hljs-string">"callback"</span></span><span>, </span><span><span class="hljs-string">"stream_notification_callback"</span></span><span>);
</span></span>

stream_notification_callback receives a parameter containing notification information, allowing developers to respond based on the different states of a stream operation.

Parameter Details:

  • resource $stream: The stream resource associated with the notification.

  • int $notification_code: The type of notification, indicated by constants such as STREAM_NOTIFY_CONNECT or STREAM_NOTIFY_DISCONNECT.

  • int $severity: The severity of the notification, usually an integer, with higher values indicating more serious issues.

  • string $message: The notification message, providing information about the current stream state.

The purpose of the callback function is to allow developers to capture and respond to these notifications. For example, if a stream connection fails, the developer can catch this information in the callback function and take appropriate actions, such as retrying the connection or logging an error.


The Relationship Between stream_notification_callback and Stream Contexts

stream_notification_callback is closely tied to stream contexts. Through the callback settings in a stream context, developers can integrate stream_notification_callback as a mechanism to handle stream notifications. Specifically, the stream_context_set_option function allows developers to assign a callback function for notification events within a stream context.

For example, when creating a network stream and setting a callback function, a developer can use the following code:

<span><span><span class="hljs-variable">$options</span></span><span> = </span><span><span class="hljs-keyword">array</span></span><span>(
    </span><span><span class="hljs-string">"notification"</span></span><span> => </span><span><span class="hljs-keyword">array</span></span><span>(
        </span><span><span class="hljs-string">"callback"</span></span><span> => </span><span><span class="hljs-string">"stream_notification_callback"</span></span><span>
    )
);
</span><span><span class="hljs-variable">$context</span></span><span> = </span><span><span class="hljs-title function_ invoke__">stream_context_create</span></span><span>(</span><span><span class="hljs-variable">$options</span></span><span>);
</span><span><span class="hljs-variable">$fp</span></span><span> = </span><span><span class="hljs-title function_ invoke__">stream_socket_client</span></span><span>(</span><span><span class="hljs-string">"tcp://example.com:80"</span></span><span>, </span><span><span class="hljs-variable">$errno</span></span><span>, </span><span><span class="hljs-variable">$errstr</span></span><span>, </span><span><span class="hljs-number">30</span></span><span>, STREAM_CLIENT_CONNECT, </span><span><span class="hljs-variable">$context</span></span><span>);
</span></span>

In the code above, the stream_socket_client() function opens a TCP connection and binds the custom callback function to the stream context. This way, if any notification events occur during the connection, PHP will automatically call stream_notification_callback.


Common Notification Types

PHP's stream_notification_callback can capture various notification types. Here are some common types and their meanings:

  • STREAM_NOTIFY_CONNECT: Indicates that the stream has successfully connected.

  • STREAM_NOTIFY_DISCONNECT: Indicates that the stream has been disconnected.

  • STREAM_NOTIFY_FAILURE: Indicates that a stream operation has failed.

  • STREAM_NOTIFY_AUTH_REQUIRED: Indicates that authentication is required for the stream operation.

Developers can write specific handling logic based on these notification types. For example, if a connection fails, a retry can be attempted; if authentication is requested, credentials can be provided, and so on.