php8.5
Home/ Manual/ stream / functions/ stream_context_create

stream_context_create

PHP function Edit on GitHub ✎

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

Creates a stream context

Description

stream_context_create(array|null $options = null, array|null $params = null): resource

Creates and returns a stream context with any options supplied in options preset.

Parameters

options

Must be an associative array of associative arrays in the format $arr['wrapper']['option'] = $value, or null. Refer to context options for a list of available wrappers and options.

Defaults to null.

params

Must be an associative array in the format $arr['parameter'] = $value, or null. Refer to context parameters for a listing of standard stream parameters.

Return Values

A stream context resource.

Changelog

VersionDescription
8.0.0options and params are now nullable.

Examples

Using stream_context_create()

php
<?php
$opts = [
  'http' => [
    'method' => "GET",
    // Use CRLF \r\n to separate multiple headers
    'header' => "Accept-language: en\r\n" .
                "Cookie: foo=bar",
  ]
];

$context = stream_context_create($opts);

/* Sends an http request to www.example.com
   with additional headers shown above */
$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>

See Also

Source: reference/stream/functions/stream-context-create.xml · from the official PHP manual (php/doc-en)