在simplexml_load_file上设置超时

时间:2012-09-13 04:54:06

标签: php timeout simplexml try-catch

我有这个输出rss feed的脚本。我想要做的是让它尝试到达5秒顶部的rss网址,如果它不能,那么我希望它加载服务器上的备份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 />';
   }
}

 ?>

我得到了一个超时错误,就是这样。任何见解都会很棒!

3 个答案:

答案 0 :(得分:1)

@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);
}

答案 1 :(得分:0)

来自php.net

<?php
$fp = fsockopen("www.example.com", 80);
if (!$fp) {
     echo "Unable to open\n";
} else {

    fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
    stream_set_timeout($fp, 2);
    $res = fread($fp, 2000);

    $info = stream_get_meta_data($fp); //I think this is what you need.
    fclose($fp);

    if ($info['timed_out']) { //So you can check this variable.
        echo 'Connection timed out!';
    } else {
        echo $res;
    }

}
?>

答案 2 :(得分:0)

这就是我的工作:

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

    ?>