目前,我正在获取远程站点的 XML 提要并在我的服务器上保存一个本地副本,以便在 PHP 中进行解析。
问题是如何在 PHP 中添加一些检查以查看 feed.xml 文件是否有效,如果有效,请使用 feed.xml。
如果因错误而无效(有时远程 XML 提要显示空白 feed.xml),是否提供来自先前抓取/保存的 feed.xml 的备份有效副本?
代码抓取 feed.xml
<?php
/**
* Initialize the cURL session
*/
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL,
'http://domain.com/feed.xml');
/**
* Create a new file
*/
$fp = fopen('feed.xml', 'w');
/**
* Ask cURL to write the contents to a file
*/
curl_setopt($ch, CURLOPT_FILE, $fp);
/**
* Execute the cURL session
*/
curl_exec ($ch);
/**
* Close cURL session and file
*/
curl_close ($ch);
fclose($fp);
?>
到目前为止只有这个来加载它
$xml = @simplexml_load_file('feed.xml') or die("feed not loading");
谢谢