libxml_set_streams_context()
Today, we will discuss the libxml_set_streams_context() function in PHP. This function is used to set the HTTP context options for libxml functions that load
The libxml_set_streams_context() function sets the stream context that libxml will use the next time it loads or writes a document over a network protocol such as HTTP, HTTPS, or FTP. It is the hook that lets you attach custom request headers, authentication credentials, timeouts, or a proxy to the implicit network request that functions like DOMDocument::load() and simplexml_load_file() perform behind the scenes.
This page explains the signature, when you actually need it, a complete working example, and the most common gotchas.
Syntax
libxml_set_streams_context(resource $context): void| Part | Description |
|---|---|
$context | A stream context resource created with stream_context_create(). |
| Returns | void — the function returns nothing. |
The context applies globally to libxml for the current request and stays in effect until you set a different one or the script ends.
When would I use it?
Most libxml loads read a local file or an already-fetched string, so you never need this function. You reach for it only when libxml itself opens the network connection — i.e. you pass a http://, https://, or ftp:// URL to a loader. Typical cases:
- The remote server requires an
Authorizationheader (Basic auth, a bearer token). - You need a custom
User-Agent, cookie, or other request header. - You must set a timeout so a slow host doesn't hang your script.
- The request has to go through an HTTP proxy.
If you fetch the XML yourself (for example with cURL or file_get_contents()) and then parse the resulting string with simplexml_load_string() or DOMDocument::loadXML(), libxml never touches the network and this function does nothing useful.
How to use it
Create the context with stream_context_create(), register it, then load your document:
<?php
// 1. Describe the network request.
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => "Authorization: Basic " . base64_encode('username:password') . "\r\n"
. "User-Agent: w3docs-example\r\n",
'timeout' => 5, // seconds
],
]);
// 2. Tell libxml to use it for the next network load.
libxml_set_streams_context($context);
// 3. Load a remote XML file. libxml opens the connection with the headers above.
$doc = new DOMDocument();
$doc->load('https://example.com/feed.xml');
echo $doc->documentElement->nodeName;
?>Because libxml swallows protocol-level errors, pair this with libxml_use_internal_errors() so you can inspect failures instead of getting silent warnings:
<?php
libxml_use_internal_errors(true);
$context = stream_context_create([
'http' => ['timeout' => 5],
]);
libxml_set_streams_context($context);
$doc = new DOMDocument();
if (!$doc->load('https://example.com/feed.xml')) {
foreach (libxml_get_errors() as $error) {
echo trim($error->message), "\n";
}
libxml_clear_errors();
}
?>See libxml_get_errors() for how to read the collected errors.
Common gotchas
- It only affects network loads. Calling it before parsing a local file or an in-memory string has no effect.
- Multiple headers must be separated by
\r\n. Theheaderoption is a single string; join lines with"\r\n"(or pass an array of strings). - Order matters. Call
libxml_set_streams_context()before theload()/simplexml_load_file()call, not after. - No
https://support? The HTTPS stream wrapper requires the OpenSSL extension to be enabled. Without it, libxml cannot openhttps://URLs at all. - Remote loading must be allowed. Some libxml security settings (and the
allow_url_fopenini directive) can block remote document loading entirely.
Related functions
libxml_use_internal_errors()— capture parser errors instead of emitting warnings.libxml_get_errors()— retrieve those captured errors.simplexml_load_file()— a SimpleXML loader that also honors the stream context.- PHP libxml overview — the broader libxml extension.
Conclusion
libxml_set_streams_context() is the bridge between PHP's stream contexts and libxml's network loaders. Whenever you load XML straight from a URL and need authentication, custom headers, a timeout, or a proxy, build a context with stream_context_create() and register it with this function before the load. For local files or pre-fetched strings, you don't need it at all.