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 $context = stream_context_create([ 'http' => ['method' => 'GET', 'timeout' => 5], ]); // file_get_contents($url, false, $context);
Try it
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.
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
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
httpoptions:method,header(a string with\r\nbetween lines, or an array),content,timeout(seconds, float),follow_location,max_redirects,ignore_errorsanduser_agent. ignore_errors: by defaultfile_get_contents()returnsfalseon a 4xx or 5xx response. Withignore_errors => trueyou 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.
ssloptions such asverify_peershould 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-Lengthcorrectly if you build the header yourself; PHP does add it when you givecontent. - Servers disable remote URLs with
allow_url_fopen=0; thenhttp://URLs cannot be opened this way.