I want to load and save an external XML
file that changes continuously eg "http://something.com/file.xml"
Sometimes the file is not available because server is overloaded or down and I want to use this file instead and re-save it if the next load is available also let people know the file that they are reading is a backup file because the original is not available.
I read about simplexml_load_file and DOMDocument and I don't know which one is the better solution.
$url = "http://www.test.com/xmlfile.xml";
$timeout = 10;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
try {
$response = curl_exec($curl);
curl_close($curl);
// success! Let's parse it and perform whatever action necessary here.
if ($response !== false) {
/** @var $xml SimpleXMLElement */
$xml = simplexml_load_string($response);
$xml->saveXML("tofile.xml");
} else {
// Warn the user here
}
} catch (Exception $ex) {
// Let user's know that there was a problem
curl_close($curl);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.