Reputation: 51
I am using DOMDocument::load()
to load a XML file from a URL:
$doc = new DOMDocument();
$doc->load("http://url_of_some_xml_file");
Is there a way to interrupt or stop loading after defined timeout if the URL took more than X seconds to load?
If this is not possible to be done using DOMDocument::load()
, is there any other way to set a timeout for loading XML from a URL?
Upvotes: 1
Views: 1736
Reputation: 85838
In addition to @thw 's excellent answer of setting the timout option by creating a new steam context, I found that the default stream context uses the php.ini
setting for default_socket_timeout
as its timeout.
Changing your php.ini
like this:
; Increase from the default of 60 seconds
default_socket_timeout = 120
can be an easy way to increase the timeout without modifying the application code, though this will also affect other stream contexts that use sockets as well.
Upvotes: 1
Reputation: 19502
You can use libxml_set_streams_context to configure the behavior. The HTTP stream wrapper options include timeout
.
$options = [
'http' => [
'method' => 'GET',
'timeout' => '5'
]
];
$context = stream_context_create($options);
libxml_set_streams_context($context);
$doc = new DOMDocument();
$doc->load("http://url_of_some_xml_file");
Upvotes: 3