Example 1 — Set a Default User Agent
Store a sample HTTP user agent on the default context and read it back. No network I/O happens.
<?php
$default = stream_context_set_default([
"http" => [
"user_agent" => "TutorialAgent/1",
"method" => "GET",
],
]);
$opts = stream_context_get_options($default);
echo $opts["http"]["user_agent"], PHP_EOL;
echo $opts["http"]["method"], PHP_EOL; How It Works
- We build a nested options array for the http wrapper.
- stream_context_set_default() applies it to the shared default context.
- get_options() reads the values back.
- The printed lines confirm user_agent and method.

