Example 1 — Set a Full HTTP Options Array
Create a context, apply several HTTP options at once, then print them. Requires PHP 8.3+.
<?php
$ctx = stream_context_create();
$options = [
"http" => [
"protocol_version" => 1.1,
"user_agent" => "PHPT Agent",
],
];
stream_context_set_options($ctx, $options);
$stored = stream_context_get_options($ctx);
echo $stored["http"]["user_agent"], PHP_EOL;
echo $stored["http"]["protocol_version"], PHP_EOL; How It Works
- An empty context is created.
- The $options array holds two http keys.
- stream_context_set_options() stores the whole array.
- get_options() reads user_agent and protocol_version back.

