PHP stream_context_create() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_context_create() builds a stream context resource that stores options and parameters for wrappers such as http, ssl, or ftp.

You pass that context as the last argument to fopen(), file_get_contents(), and similar functions. Contexts let you set things like HTTP method, headers, or SSL settings without changing global defaults.

Syntax

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

PHP
$ctx = stream_context_create(["http" => ["method" => "GET"]]);
CallWhat it does
stream_context_create()Creates an empty context you can fill later with set_option or set_options.
stream_context_create($options)Creates a context already loaded with wrapper options.
stream_context_create($options, $params)Creates a context with both options and parameters such as a notification callback.

Options use a nested array shape: $options['wrapper']['option'] = $value. For HTTP and HTTPS requests the wrapper key is still "http". These tutorials only inspect contexts offline; they do not open live network URLs.

Library declarations
PHP
stream_context_create(?array $options = null, ?array $params = null): resource

Parameters

ParameterDescription
optionsOptional. Nested array of wrapper options, or null for none.
paramsOptional. Associative array of context parameters (for example notification), or null.

Return Value

Return ValueDescription
resourceA stream context resource you can pass to file functions or inspect with get_options / get_params.

PHP Examples

Example 1 — Create a Context and Read Its Options

Build a context with a sample HTTP user agent, then print the stored options. No network call is made.

PHP
<?php
$options = [
    "http" => [
        "method" => "GET",
        "user_agent" => "CodeToFun/1",
    ],
];

$ctx = stream_context_create($options);
$stored = stream_context_get_options($ctx);

echo $stored["http"]["method"], PHP_EOL;
echo $stored["http"]["user_agent"], PHP_EOL;

How It Works

  1. The $options array uses the nested wrapper/option shape.
  2. stream_context_create() turns that array into a context resource.
  3. stream_context_get_options() reads the options back for display.
  4. We print method and user_agent to confirm they were stored.

Example 2 — Create an Empty Context First

You can create a blank context and add options later with stream_context_set_option().

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");

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

How It Works

  1. stream_context_create() with no arguments starts empty.
  2. stream_context_set_option() sets one option at a time.
  3. get_options() returns the nested array currently stored.
  4. The printed lines show POST and the Content-Type header.

Example 3 — Include Context Parameters

Pass a params array as the second argument. Here we store a notification callback name and then list the param keys.

PHP
<?php
$options = [
    "http" => [
        "method" => "GET",
    ],
];
$params = [
    "notification" => "stream_notification_callback",
];

$ctx = stream_context_create($options, $params);
$info = stream_context_get_params($ctx);

echo $info["notification"], PHP_EOL;
echo isset($info["options"]["http"]["method"]) ? $info["options"]["http"]["method"] : "", PHP_EOL;

How It Works

  1. Options and params are separate arrays.
  2. stream_context_create() stores both on the new context.
  3. stream_context_get_params() returns parameters plus an options key.
  4. We print the notification name and the HTTP method from options.

Frequently Asked Questions

It creates a context resource that holds options and parameters for stream wrappers such as http or ssl.
Pass it as the context argument to fopen(), file_get_contents(), file(), or readfile() when those functions support it.
Use $options['wrapper']['option'] = $value, for example $options['http']['user_agent'] = 'MyApp/1'.
No. The context only stores settings until a file or stream function uses it.
Options configure wrappers (HTTP headers, SSL flags, and similar). Params are context-level settings such as a notification callback.

Next: stream_context_get_default()

stream_context_get_default() returns the default stream context used when file functions a.

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