Current Location: Home> Latest Articles> Use stream_context_create() and stream_context_get_options() to manage HTTP requests

Use stream_context_create() and stream_context_get_options() to manage HTTP requests

M66 2025-05-28

In PHP, stream_context_create() and stream_context_get_options() are useful functions for managing and viewing HTTP request configurations. They can help developers configure network requests through context, especially when using file_get_contents() or other stream manipulation functions, to control the parameters and behavior of the request. In this article, we will show you how to use these two functions to manage and view configuration options for HTTP requests with simple examples.

1. Create a context using stream_context_create()

stream_context_create() is used to create a stream context that can specify custom options for HTTP requests, such as proxy server, request header, timeout time, etc.

Sample code:

 <?php
// Set requested HTTP Options
$options = [
    'http' => [
        'method'  => 'GET',
        'header'  => "Accept-language: en\r\n",
        'timeout' => 60, // Set the request timeout to60Second
    ]
];

// Create a HTTP Context
$context = stream_context_create($options);

// 通过Context发起 HTTP ask
$url = "http://m66.net/some/path";
$response = file_get_contents($url, false, $context);

// Output response content
echo $response;
?>

In the above code, we create a context containing HTTP configuration via stream_context_create() . In this context, we set the HTTP method of the request to GET and set the request header and timeout.

2. Use stream_context_get_options() to view configuration options

Once you have created a context, you can use stream_context_get_options() to view the configuration options in the current context. This is very useful for debugging and verifying configurations.

Sample code:

 <?php
// 获取当前Context的配置Options
$options = stream_context_get_options($context);

// 输出配置Options
echo '<pre>';
print_r($options);
echo '</pre>';
?>

In the above code, stream_context_get_options() returns an array containing the current context configuration information. This array displays all HTTP configuration options set via stream_context_create() .

3. Example: Send a POST request and view the configuration

In addition to GET requests, you can also use stream_context_create() to send POST requests, pass data, and view related configurations.

Sample code:

 <?php
// set up POST ask的 HTTP Options
$options = [
    'http' => [
        'method'  => 'POST',
        'header'  => "Content-Type: application/x-www-form-urlencoded\r\n",
        'content' => http_build_query(['key1' => 'value1', 'key2' => 'value2']),
        'timeout' => 60, // Set the request timeout to60Second
    ]
];

// 创建Context
$context = stream_context_create($options);

// send POST ask
$url = "http://m66.net/api/submit";
$response = file_get_contents($url, false, $context);

// Output response content
echo $response;

// 获取并显示当前配置Options
$options = stream_context_get_options($context);
echo '<pre>';
print_r($options);
echo '</pre>';
?>

4. Common HTTP configuration options

stream_context_create() supports many HTTP configuration options. Here are some common configuration options:

  • method : HTTP methods (such as GET, POST, PUT, DELETE, etc.)

  • header : The request header is specified in string form (such as Content-Type , User-Agent , etc.)

  • content : The content sent (mainly used for POST requests)

  • timeout : Request timeout (unit: seconds)

  • proxy : proxy server settings

  • user_agent : Set the User-Agent header

Conclusion

stream_context_create() and stream_context_get_options() provide a simple and powerful way to manage and view configuration options for HTTP requests. With these tools, you can easily set custom options for streaming operations in PHP such as file_get_contents() for more efficient and flexible network requests. Mastering these methods will help you gain better control over HTTP requests and debug your applications.