Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加载外部XML并使用PHP保存_Php_Xml - Fatal编程技术网

加载外部XML并使用PHP保存

加载外部XML并使用PHP保存,php,xml,Php,Xml,我想加载并保存一个不断变化的外部XML文件 e、 g.“http://something.com/file.xml" 有时,由于服务器过载或停机,文件不可用。我想改用此文件,如果下一次加载可用,则重新保存该文件。此外,还可以让人们知道他们正在读取的文件是备份文件,因为原始文件不可用 我读过simplexml\u load\u文件和DOMDocument,不知道哪一个是更好的解决方案。你的问题不是很清楚。您打算如何“让人们知道”(修改XML?让它从信息页面下载?…)?。此外,工作流程也不清楚:如果

我想加载并保存一个不断变化的外部
XML
文件 e、 g.“http://something.com/file.xml"

有时,由于服务器过载或停机,文件不可用。我想改用此文件,如果下一次加载可用,则重新保存该文件。此外,还可以让人们知道他们正在读取的文件是备份文件,因为原始文件不可用


我读过simplexml\u load\u文件和DOMDocument,不知道哪一个是更好的解决方案。

你的问题不是很清楚。您打算如何“让人们知道”(修改XML?让它从信息页面下载?…)?。此外,工作流程也不清楚:如果没有新文件,为什么要“再次保存”以前的版本,而不是直接使用它?(我相信你有充分的理由;我是说你没有说清楚)。然而,要下载XML,我建议使用带有超时和带宽选项的cURL;为了验证这一点,我从SimpleXML开始。这就成功了,正如@lserni所说的,cURL就是方法,谢谢你们,对不起,如果我没有解释清楚,我的英语很差。再次感谢你。
$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);
}