Replacing simplexml_load_file with PHP cURL
The PHP XML data loading function simplexml_load_file has a very short timeout which apparently isn’t controllable (or documented). Replacing it with cURL and simplexml_load_string is relatively straightforward, but for reference:
Original simplexml_load_file
<?php
$path = 'http://example.org/path/to/xml';
$xml === False on failure
$xml = @simplexml_load_file($path);
?>
Replaced with cURL and simplexml_load_string
<?php
$path = 'http://example.org/path/to/xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$returned = curl_exec($ch);
curl_close($ch);
// $xml === False on failure
$xml = simplexml_load_string($returned);
?>
Oh PHP, why do you? I don’t even.