Start · Sprachen · PHP · Referenz · stream_context_create

stream_context_create

Funktion

Erzeugt einen Stream-Context für <code>file_get_contents()</code>, <code>fopen()</code> und andere Stream-Funktionen — mit Timeout, Headers, SSL-Optionen.

seit PHP 4.3.0 Kategorie: io

Signatur

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

Beschreibung

Options sind nach Wrapper geschachtelt: ['http' => [...], 'ssl' => [...]].

Parameter

Name Typ Default Beschreibung
$options ?array null Wrapper-Optionen.
$params ?array null Weitere Parameter.

Rückgabewert

Typ
resource
Beschreibung
Context-Handle.

Beispiele

HTTP mit Timeout und Headers

<?php

$ctx = stream_context_create([
    'http' => [
        'timeout' => 5,
        'header'  => "Accept: application/json\r\nUser-Agent: programmierung.net\r\n",
        'method'  => 'GET',
    ],
]);
$json = file_get_contents($url, context: $ctx);