我有此脚本输出rss提要。我想要做的是让它尝试到达rss url 5秒钟之类的时间,如果不能,那么我希望它加载服务器上的备份xml文档。这是我所拥有的,并且无法正常工作:
<?php
include '../php/connect.php';
$metaData = mysql_query("SELECT * FROM `siteinfo`") or die("couln't find table :(");
$displayData = mysql_fetch_assoc($metaData);
$url = $displayData['status'];
$xml = file_get_contents($url);
stream_set_timeout($xml, 5);
if ($xml == FALSE) {
$xml = simplexml_load_file('backUpXml.xml');
foreach ($xml->channel->item as $item) {
echo '<a href="'.$item->guid.'" alt="'.$item->title.'" target="_blank">', substr($item->title, 0, 62), '...</a><br /><span>', substr($item->pubDate, 4, 18),'</span><br /><hr /><br />';
}
} else {
$xml = simplexml_load_file($url);
foreach ($xml->channel->item as $item) {
echo '<a href="'.$item->guid.'" alt="'.$item->title.'" target="_blank">', substr($item->title, 0, 62), '...</a><br /><span>', substr($item->pubDate, 4, 18),'</span><br /><hr /><br />';
}
}
?>
我收到超时错误,仅此而已。任何见识都将是伟大的!
最佳答案
@AnthonySterling here指出了一个更好的解决方案:
function simplexml_load_file_from_url($url, $timeout = 5){
$opts = array('http' => array('timeout' => (int)$timeout));
$context = stream_context_create($opts);
$data = file_get_contents($url, false, $context);
if(!$data){
trigger_error('Cannot load data from url: ' . $url, E_USER_NOTICE);
return false;
}
return simplexml_load_string($data);
}
关于php - 在simplexml_load_file上设置超时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12399610/