目前,我正在获取远程站点的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");
谢谢