PHP stream_context_set_options() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_context_set_options() sets the full nested options array on a stream context in one call. It was added in PHP 8.3.

Use it when you already have an $options['wrapper']['option'] structure. It replaces the old two-argument stream_context_set_option($context, $array) form, which is deprecated in PHP 8.4.

Syntax

Call stream_context_set_options() with the values you need. Start with this line.

PHP
stream_context_set_options($ctx, ["http" => ["method" => "GET"]]);
CallWhat it does
stream_context_set_options($ctx, $options)Writes the whole nested options array onto the context.
stream_context_set_options($ctx, ["ssl" => ["verify_peer" => true]])Sets one wrapper section by passing a full options array.
stream_context_get_options($ctx)Typical follow-up to verify what was stored.

Needs PHP 8.3 or later. On older versions, either upgrade or fall back to repeated stream_context_set_option() four-argument calls, or the legacy two-argument set_option array form.

Library declarations
PHP
stream_context_set_options(resource $context, array $options): true

Parameters

ParameterDescription
contextRequired. The stream or context resource to update.
optionsRequired. Nested array in $array['wrapper']['option'] = $value form.

Return Value

Return ValueDescription
trueReturns true on success.

PHP Examples

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
<?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

  1. An empty context is created.
  2. The $options array holds two http keys.
  3. stream_context_set_options() stores the whole array.
  4. get_options() reads user_agent and protocol_version back.

Example 2 — Set Options for Two Wrappers

Pass http and ssl sections together in one set_options() call.

PHP
<?php
$ctx = stream_context_create();
stream_context_set_options($ctx, [
    "http" => [
        "method" => "GET",
    ],
    "ssl" => [
        "verify_peer" => true,
    ],
]);

$opts = stream_context_get_options($ctx);
echo implode(",", array_keys($opts)), PHP_EOL;
echo $opts["http"]["method"], PHP_EOL;
echo $opts["ssl"]["verify_peer"] ? "ssl=1" : "ssl=0", PHP_EOL;

How It Works

  1. One nested array can include multiple wrappers.
  2. set_options() applies both sections in a single call.
  3. array_keys() lists http and ssl.
  4. We print the method and a verify flag.

Example 3 — Replace Options From a Prepared Array

Build the options array first, set it, then confirm a content body option was stored.

PHP
<?php
$ctx = stream_context_create();
$body = "name=Ada";
$options = [
    "http" => [
        "method" => "POST",
        "header" => "Content-Type: application/x-www-form-urlencoded",
        "content" => $body,
    ],
];

stream_context_set_options($ctx, $options);
$http = stream_context_get_options($ctx)["http"];

echo $http["method"], PHP_EOL;
echo $http["content"], PHP_EOL;
echo strlen($http["header"]) > 0 ? "header_set" : "header_missing", PHP_EOL;

How It Works

  1. The body string is prepared before building $options.
  2. set_options() stores method, header, and content together.
  3. get_options() returns the http section for checks.
  4. The sample output shows POST, the body, and that a header exists.

Frequently Asked Questions

It sets a full nested options array on a context. It is the array-based setter added in PHP 8.3.
set_options() takes an options array. set_option() takes wrapper, option name, and value for a single option.
PHP 8.3.0. On older versions the function does not exist.
No on PHP 8.4+, where that two-argument form is deprecated. Prefer stream_context_set_options().
Use $array['wrapper']['option'] = $value, the same shape as stream_context_create().

Next: stream_context_set_params()

stream_context_set_params() stores context parameters such as a notification callback on a.

stream_context_set_params() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful