0
     <?php
     define(feed,'http://www.example.com/feed/');
     $xml_feed = file_get_contents(feed);
     $xml_parser = xml_parser_create();
     xml_parse_into_struct($xml_parser,$xml_feed,$xml_keys,$xml_index);
     xml_parser_free($xml_parser);
     for($i = 0;!empty($xml_index['TITLE'][$i]);$i++){
     if ($counter < 10)
     {
     echo '<span class="nieuwsfeed">
            <a href="'.$xml_keys[$xml_index['LINK'][$i]]['value'].'" target="blank_">' . date('d-m-Y', strtotime($xml_keys[$xml_index['PUBDATE'][$i]]['value'])) . ' ' . substr($xml_keys[$xml_index['TITLE'][$i]]['value'], 0, 45).'</a></span>';
     $counter++;
     }}
     ?>

.

 $doc = new DOMDocument();
 $doc->load('http://feeds.example.com/example');
 $arrFeeds = array();
 foreach ($doc->getElementsByTagName('item') as $node) {
  $itemRSS = array ( 
   'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
  'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
  'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
  'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
  );
array_push($arrFeeds, $itemRSS);
 }
 foreach ($arrFeeds as $row)
{
     if ($counter2 < 10)
{
?>

This error is showing when the feed is offline.

      Warning: DOMDocument::load(http://feeds.example.com/example)   
      [domdocument.load]: failed to open stream: Connection timed out in

Im using this script to let my feeds run in my webpage, but IF the feed is offline, its searching for the feed for about 30 seconds till 60 seconds, does anyone has an idea how I should disable loading after an x amount of seconds and then showing just an standard image where it says the feed is currently offline.

Its about 2/3 times per week one of the XML feeds are offline, so it's pretty annoying if your webpage keeps loading.

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
Mr chinpansee
  • 379
  • 1
  • 3
  • 19

1 Answers1

0

You can use file_get_contents() and define the timeout using a stream context as explained here: Does file_get_contents() have a timeout setting?

Modified code from the answer there:

<?php
$ctx=stream_context_create(array('http'=>
    array(
        'timeout' => 5 // 5 seconds
    )
));

echo file_get_contents('http://example.com/',false,$ctx);
?>
Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088