0

XML「http://something.com/file.xml」など、継続的に変化する外部ファイルを読み込んで保存したい

サーバーが過負荷またはダウンしているためにファイルが利用できない場合があり、代わりにこのファイルを使用し、次の読み込みが利用可能になった場合に再保存したい また、元のファイルがバックアップファイルではないため、読んでいるファイルがバックアップファイルであることを人々に知らせます利用可能。

simplexml_load_file と DOMDocument について読みましたが、どちらがより良い解決策かわかりません。

4

1 に答える 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);
}
于 2012-12-11T22:27:09.820 に答える