0

I want to load and save an external XML file that changes continuously e.g. "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.

1
  • 2
    Your question is not really clear. How do you plan to "let people know" (Modify the XML? Let it be downloaded from an info page? ...)?. Also, the workflow is not clear: if you don't have the new file, why "save again" the previous version instead of using it directly? (I'm sure you have a good reason; I'm saying that you did not make it clear). To download the XML, however, I'd suggest cURL with timeout and bandwidth options; to validate it, I'd start with SimpleXML. Commented Dec 11, 2012 at 22:25

1 Answer 1

1
$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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick, and as @lserni said, cURL is the way, thank you guys, sorry if i didnt explain it clear, my english is bad. Thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.