stream_context_get_default

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

stream_context_get_defaultRecuperar el contexto de flujo predeterminado

Descripción

stream_context_get_default(array $options = ?): resource

Devuelve el contexto de flujo predeterminado que es usado siempre que las operaciones con archivos (fopen(), file_get_contents(), etc...) son llamadas sin un parámetro de contexto. Las opciones para el contexto predeterminado pueden ser especificadas opcionalmente con esta función usando la misma sintaxis que en stream_context_create().

Parámetros

options
options debe ser una matriz asociativa de matrices asociativas con el formato $matriz['envoltura']['opción'] = $valor.

Nota:

A partir de PHP 5.3.0, la función stream_context_set_default() se puede usar para establecer el contexto predeterminado.

Valores devueltos

Un resource de contexto de flujo.

Ejemplos

Ejemplo #1 Usar stream_context_get_default()

<?php
$opc_pred
= array(
'http'=>array(
'método'=>"GET",
'cabecera'=>"Accept-language: en\r\n" .
"Cookie: foo=bar",
'proxy'=>"tcp://10.54.1.39:8000"
)
);


$opc_alt = array(
'http'=>array(
'método'=>"POST",
'cabecera'=>"Content-type: application/x-www-form-urlencoded\r\n" .
"Content-length: " . strlen("baz=bomb"),
'contenido'=>"baz=bomb"
)
);

$predeterminado = stream_context_get_default($opc_pred);
$alternativo = stream_context_create($opc_alt);

/* Envía una petición GET normal al servidor proxy en 10.54.1.39
* Para www.example.com usar opciones de contexto especificadas en $opc_pred
*/
readfile('http://www.example.com');

/* Envía una petición POST directamente a www.example.com
* Usar opciones de contexto especificadas en $opc_alt
*/
readfile('http://www.example.com', false, $alternativo);

?>

Ver también

add a note add a note

User Contributed Notes 1 note

up
0
RQuadling at GMail dot com
17 years ago
If you are using stream_context_get_default() and are still finding that some functions do not work, make sure that they are not based upon the libxml functions (DOM, SimpleXML and XSLT). These require their own context.

You can easily set them using the following code ...

<?php
// Define the default, system-wide context.
$r_default_context = stream_context_get_default
   
(
    array
        (
       
'http' => array
            (
// All HTTP requests are passed through the local NTLM proxy server on port 8080.
           
'proxy' => 'tcp://127.0.0.1:8080',
           
'request_fulluri' => True,
            ),
        )
    );

// Though we said system wide, some extensions need a little coaxing.
libxml_set_streams_context($r_default_context);
?>
To Top