PHP stream_context_set_option() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_context_set_option() sets one wrapper option on a stream or context, such as http/user_agent or ssl/verify_peer.

The four-argument form is the supported way to set a single option. An older two-argument form that accepted a full options array still exists but is deprecated as of PHP 8.4; use stream_context_set_options() for whole arrays instead.

Syntax

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

PHP
stream_context_set_option($ctx, "http", "user_agent", "App/1");
CallWhat it does
stream_context_set_option($ctx, "http", "method", "GET")Sets one HTTP option named method.
stream_context_set_option($ctx, "ssl", "verify_peer", true)Sets one SSL option named verify_peer.
stream_context_set_option($fp, "http", "header", "Accept: text/plain")Sets an option on a stream resource that already has a context.

Signature reminder: set_option = one option (wrapper, name, value). set_options = full nested options array (PHP 8.3+). Avoid the deprecated set_option($ctx, $array) form on PHP 8.4+.

Library declarations
PHP
stream_context_set_option(resource $stream_or_context, string $wrapper, string $option_name, mixed $value): bool

Parameters

ParameterDescription
stream_or_contextRequired. The stream or context resource to update.
wrapperRequired. Wrapper name such as "http" or "ssl".
option_nameRequired. Option name for that wrapper.
valueRequired. Value to store for the option.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Set One HTTP Option

Create an empty context, set user_agent, then read it back with get_options().

PHP
<?php
$ctx = stream_context_create();
$ok = stream_context_set_option($ctx, "http", "user_agent", "Single/1");

$opts = stream_context_get_options($ctx);
echo $ok ? "ok" : "fail", PHP_EOL;
echo $opts["http"]["user_agent"], PHP_EOL;

How It Works

  1. An empty context is created first.
  2. set_option() stores http/user_agent in one call.
  3. The boolean return value is true on success.
  4. get_options() proves the value is Single/1.

Example 2 — Set Several Options One by One

Call set_option() three times to build a small HTTP options set without using the array form.

PHP
<?php
$ctx = stream_context_create();
stream_context_set_option($ctx, "http", "method", "POST");
stream_context_set_option($ctx, "http", "header", "Content-Type: text/plain");
stream_context_set_option($ctx, "http", "content", "hello");

$http = stream_context_get_options($ctx)["http"];
echo $http["method"], PHP_EOL;
echo $http["header"], PHP_EOL;
echo $http["content"], PHP_EOL;

How It Works

  1. Each call targets the http wrapper with a different option name.
  2. Later calls add more keys without wiping earlier ones.
  3. get_options() returns the combined http section.
  4. We print method, header, and content.

Example 3 — Set an SSL Flag Offline

Store verify_peer on the ssl wrapper. This only updates local context data.

PHP
<?php
$ctx = stream_context_create();
stream_context_set_option($ctx, "ssl", "verify_peer", false);
stream_context_set_option($ctx, "ssl", "verify_peer_name", false);

$ssl = stream_context_get_options($ctx)["ssl"];
echo $ssl["verify_peer"] ? "peer=1" : "peer=0", PHP_EOL;
echo $ssl["verify_peer_name"] ? "name=1" : "name=0", PHP_EOL;

How It Works

  1. ssl is a separate wrapper key from http.
  2. Two set_option() calls store related TLS flags.
  3. get_options() reads the ssl section.
  4. The printed flags show both values are false in this sample.

Frequently Asked Questions

It sets a single wrapper option on a stream or context using the wrapper name, option name, and value.
set_option sets one option with four arguments. set_options (PHP 8.3+) sets a full nested options array in one call.
As of PHP 8.4, stream_context_set_option($context, $array) is deprecated. Use stream_context_set_options($context, $array) instead.
Yes. Pass the stream resource as the first argument when that stream has a context.
No. It only updates local context settings until a stream function uses them.

Next: stream_context_set_options()

stream_context_set_options() sets the full nested options array on a stream context in one.

stream_context_set_options() 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