在simplexml_load_file上设置超时 [英] Set time out on simplexml_load_file

查看:90
本文介绍了在simplexml_load_file上设置超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输出rss feed的脚本.我想要做的是让它尝试到达rss url 5秒钟之类的时间,如果不能,那么我希望它加载服务器上的备份xml文档.这是我所拥有的,并且无法正常工作:

I have this script which outputs an rss feed. Want I want to do is have it attempt to reach the rss url for something like 5 sec tops, and if it cannot then I want it to load a backup xml doc that is on the server. This is what I have and it is not working:

 <?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 />';
   }
}

 ?>

我收到超时错误,仅此而已.任何见识都会很棒!

I am getting a time out error and that is all. Any insight would be great!

推荐答案

这就是我正在工作的内容:

This is what I got working:

    <?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);

   if (!$xml) {

       $xml = simplexml_load_file('content/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 {

       $myFile = "content/backUpXml.xml";
       $fh = fopen($myFile, 'w') or die("can't open file");
       $stringData = $xml;
       fwrite($fh, $stringData);
       fclose($fh);

       $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 />';
       }
    }

    ?>

这篇关于在simplexml_load_file上设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆