Php load xml loading a remote rss faster or a substitute

Hi all,

I have the php code below, which takes info from a .rss file and prints it.

The rss file, is not a local file (not like in the example below), it's placed in another server.

The php code loads when my webpage load  and if the rss it's not ready or the connection is too slow or  the rss  it's not ready, the php won't load the rss information. It'll show a blank space.

What can I do for this not to happen? What can I do for the php to load the rss faster or if the rss server is slow or down to load a local file with some other info? (everything but a blank space)

Thanks a lot
<?php 
$doc = new DOMDocument();
  $doc->load( 'note.rss' ); // not a local file, just an example
  
  $items = $doc->getElementsByTagName( "item" );

 foreach( $items as $item )
  {

  $titles = $item->getElementsByTagName( "title" );
  $title = $titles->item(0)->nodeValue;
  
 echo"$title";
 }
?>


//////////////////// note.rss (the remote file )///////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:georss="http://www.georss.org/georss">
  <channel>
    <title>Title</title>
    <link>http://page.com/folder</link>
    <description>updates</description>
    <language>en-us</language>
    <ttl>40</ttl>
  <item>
    <title>title line 1 </title>
    </item>
  <item>
    <title> title line 2</title>
  </item>
</channel>
</rss>

Open in new window

Dada44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HackneyCabCommented:
Well, I don't know why your script shows a blank page if the connection is slow (possibly your script is exceeding your server's timeout limit, but that's usually thirty seconds, and I'm hoping no connection is that slow).

However, for scripts that use network resources or are expensive (in time, for instance) to execute, you should look into using caching. The PEAR extension Cache_Lite is excellent:

http://pear.php.net/package/Cache_Lite

It takes a little while to install and use in your code, but once you've got it going, you can use Cache_Lite to save a copy of your HTML page, and then simply load from that cached copy for as many seconds, minutes or hours as you think suitable. For RSS feeds, I think that caching the data for an hour or two is fine, though you can choose whatever value suits your site and the feed. An investor news feed may need to be more urgent, while a feed that's only updated every week may be suitable to cache for many hours.
0
Dada44Author Commented:
Thanks a lot for answering HackneyCab.

The rss is a twitter rss so it's probable that the issue is a time out connection one.
I'm working on the pear libraries and code  but as I'm such a newbie with php it's going to take me long :(

Any other way (event if it is not too smart) of loading a local rss or file containing a fake rss feed if the twitter one hasn't loaded after let's say five seconds? The rss contains just some news from my channel, it's not an investors feed or something that needs to be frequently updated.. the important thing is not to leave the space for the rss in my webpage blank if the twitter rss does not answer.

Thanks a ton once again!
0
HackneyCabCommented:
Well, you can increase or decrease the amount of time PHP will run a script before it terminates with an error:

http://php.net/manual/en/function.set-time-limit.php

but once that time runs out, the whole script terminates, you can't (to the best of my knowledge) set a timeout on a specific command. (PHP is not a multi-threaded scripting language, so you can't have one thread keeping an eye on the time while another thread tries to download the RSS file.)

However, if you're using cURL to download the RSS file (and I recommend using cURL instead of fopen because cURL offers rich functionality) then you can set the option CURLOPT_TIMEOUT to tell cURL how long to run before giving up. You can then check whether this happened by using curl_errorno (a timeout is error code 28 according to http://curl.haxx.se/libcurl/c/libcurl-errors.html ). In that case, you could switch to a local copy of the last downloaded feed (which your script writes to your server using file functions).
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

HackneyCabCommented:
Oh, and the link to the cURL manual pages is:

http://php.net/manual/en/book.curl.php
0
Ray PaseurCommented:
You might want to consider using SimpleXML instead of DomDocument.  I find it easier to code and understand.

If you can post the ACTUAL url of the RSS you want to load, I can show a code sample using CURL to retrieve the RSS, or using a default XML string when the RSS is unavailable.  I can also show you the easiest way to parse the RSS and display the output.

Best regards, ~Ray
0
Dada44Author Commented:
Ray, could you please show the code sample so I can test it and see if it can be a solution .. I'm getting lost with curl .. I'm too new to it :(
Thanks!
0
Ray PaseurCommented:
Sure - I would be glad to help.  What is the URL?  Thanks, ~Ray
0
Dada44Author Commented:
The url for the rss is:
http://twitter.com/statuses/user_timeline/70640679.rss

And the code I'm using is the one posted at the beginnig of this thread, so:
 $doc->load( 'http://twitter.com/statuses/user_timeline/70640679.rss' );

Thanks a ton!
0
Ray PaseurCommented:
You can install this and run it to see the moving parts.  I had some funny characters in it when I ran it - possibly something related to UTF-8 decoding.  But overall, it seemed to work OK.  Best, ~Ray
<?php // RAY_temp_rss.php
error_reporting(E_ALL);
echo "<pre>\n";

// THE TEST DATA URL
$url = 'http://twitter.com/statuses/user_timeline/70640679.rss';

// READ THE RSS INTO AN OBJECT
$obj = SimpleXML_Load_File($url);

// ACTIVATE THIS TO SEE THE ENTIRE OBJECT
// var_dump($obj);

// ITERATE OVER THE OBJECT TO FIND INFORMATION ABOUT THE VOLCANO
$volcano = strtolower('Eyjafjallajoekull');
foreach ($obj->channel->item as $item)
{
    $title = ' ' . strtolower($item->title);
    if (strpos($title, $volcano))
    {
        $link = (string)$item->link;
        echo PHP_EOL . 'VOLCANO: ' . '<a target="_blank" href="' . $link . '">' . (string)$item->title . '</a>';
    }

}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dada44Author Commented:
thanks!! and sorry for delay!
0
Ray PaseurCommented:
Thanks for the points!  Best regards, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.