PHP libxml_set_streams_context() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
libxml

Definition

In PHP, libxml_set_streams_context() sets the stream context that libxml uses when it opens files or URLs during the next XML load.

A stream context holds options such as HTTP timeouts, headers, or SSL settings. This is useful when your XML comes from a web address and you want to control how it is fetched.

Syntax

Call libxml_set_streams_context() with the values you need. Start with this line.

PHP
libxml_set_streams_context($context);
CallWhat it does
libxml_set_streams_context(stream_context_create())Sets an empty default context for upcoming loads.
libxml_set_streams_context(stream_context_create(['http' => ['timeout' => 5]]))Applies a five-second HTTP timeout to later XML loads.
libxml_set_streams_context($ctx)Reuses a context you already created with stream_context_create().

The context is created with stream_context_create(). It affects functions like simplexml_load_file(), DOMDocument::load(), and XMLReader::open() that read from streams.

Library declarations
PHP
libxml_set_streams_context(resource $context): void

Parameters

ParameterDescription
contextRequired. A stream context resource created with stream_context_create().

Return Value

Return ValueDescription
voidNo value is returned.

PHP Examples

Example 1 — Set a Context and Load a File

Create a stream context, register it with libxml, then load a local XML file.

PHP
<?php
$file = tempnam(sys_get_temp_dir(), 'xml');
file_put_contents($file, '<page><title>Context Demo</title></page>');

$context = stream_context_create();
libxml_set_streams_context($context);

$xml = simplexml_load_file($file);
echo "Loaded: ", $xml->title, PHP_EOL;

unlink($file);

How It Works

  1. We write a small XML document to a temporary file.
  2. stream_context_create() makes a default stream context.
  3. libxml_set_streams_context() tells libxml to use it for future stream access.
  4. simplexml_load_file() reads the file and we print the title.

Example 2 — Invalid Argument Throws TypeError

Only a stream context resource is accepted. Anything else raises an error in PHP 8.

PHP
<?php
try {
    libxml_set_streams_context('not a context');
} catch (TypeError $e) {
    echo "TypeError caught", PHP_EOL;
}

$context = stream_context_create();
echo is_resource($context) ? "Valid context" : "Invalid", PHP_EOL;

How It Works

  1. A plain string is not a stream context, so PHP throws a TypeError.
  2. The catch block prints a short message instead of crashing.
  3. stream_context_create() returns a proper resource.
  4. is_resource() confirms it is a valid context for the function.

Example 3 — Add an HTTP Timeout

Create a context with a timeout option, check it, and register it for remote XML loads.

PHP
<?php
$context = stream_context_create([
    'http' => [
        'timeout' => 3,
        'method'  => 'GET'
    ]
]);

libxml_set_streams_context($context);

$options = stream_context_get_options($context);
echo "Timeout: ", $options['http']['timeout'], PHP_EOL;
echo "Method: ", $options['http']['method'], PHP_EOL;

How It Works

  1. The options array sets a three-second timeout and the GET method for http streams.
  2. stream_context_create() wraps those options in a context resource.
  3. libxml_set_streams_context() registers it so later remote loads follow these rules.
  4. stream_context_get_options() reads the settings back so we can print them.

Frequently Asked Questions

It sets the stream context that libxml uses when it opens files or URLs for XML parsing. This lets you control things like timeouts and headers.
Use stream_context_create() and pass an array of options, for example ['http' => ['timeout' => 5]]. Then give the result to libxml_set_streams_context().
No. It returns void, so there is nothing to check after calling it.
Functions that load XML from streams, such as simplexml_load_file(), DOMDocument::load(), DOMDocument::loadHTMLFile(), and XMLReader::open().
PHP 8 throws a TypeError. Always pass the resource returned by stream_context_create().

Next: libxml_use_internal_errors()

libxml_use_internal_errors() turns libxml's internal error handling on or off. When it is .

libxml_use_internal_errors() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful