Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Files and I/O

PHP stream contexts

Reference for PHP stream contexts: stream_context_create(), wrapper options for http and file, timeouts, headers, default contexts and reading options back.

A stream context is a bundle of settings that changes how a stream wrapper behaves. You create one with stream_context_create() and pass it as the last argument to functions such as fopen(), file_get_contents(), file() and copy(). Typical uses are sending an HTTP method, headers and a body, setting a timeout, or turning off redirect following.

Options are grouped by wrapper name (http, ssl, file, ...) and each group is a map of option names to values.

PHP
<?php
$context = stream_context_create([
    'http' => ['method' => 'GET', 'timeout' => 5],
]);
// file_get_contents($url, false, $context);

Try it

PHP Creating a context and reading it back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Contexts accept any wrapper, here with a local file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
stream_context_create(?array $options = null, ?array $params = null): resource
stream_context_get_options(resource $stream_or_context): array
stream_context_set_option(resource $context, string $wrapper, string $option, mixed $value): bool
stream_context_set_default(array $options): resource
Name Description
$options Nested array: [wrapper => [option => value]].
$params Optional parameters such as a notification callback.
$wrapper, $option, $value A single option to change on an existing context.

Return value: stream_context_create() returns a context resource. stream_context_get_options() returns the options array.

Errors / Exceptions: an options array that is not nested as [wrapper => [option => value]] raises a ValueError.

Usage notes

  • Common http options: method, header (a string with \r\n between lines, or an array), content, timeout (seconds, float), follow_location, max_redirects, ignore_errors and user_agent.
  • ignore_errors: by default file_get_contents() returns false on a 4xx or 5xx response. With ignore_errors => true you get the body, and the status line is in $http_response_header.
  • Always set a timeout on network calls so a slow server cannot hang your script.
  • ssl options such as verify_peer should stay on. Disabling certificate checks to "make it work" removes the protection of HTTPS.
  • Default context: stream_context_set_default() applies options to every stream that is opened without an explicit context.
  • Not an HTTP client. For anything more than a simple request (retries, connection reuse, uploads), use cURL or a client library. This extension may be unavailable in some environments.
  • Request bodies: you must set Content-Length correctly if you build the header yourself; PHP does add it when you give content.
  • Servers disable remote URLs with allow_url_fopen=0; then http:// URLs cannot be opened this way.