Re: XML/RSS stopped working suddenly

From: Date: Sat, 06 Nov 2004 15:14:50 +0000
Subject: Re: XML/RSS stopped working suddenly
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-15368@lists.php.net to get a copy of this message
Stowe Spivey wrote: [...]
Did you upgrade from PHP4 to PHP5 in the meantime? I had the same problem when I upgraded to PHP5, but it's extremely trivial to rewrite your code to use SimpleXML instead of XML_RSS, just a few lines change.
Thanks but no I have not upgraded or changed a thing. Very strange that I can no longer get any RSS to work in it. Can you tell what I need to change to move to SimpleXML, or provide a link with a sample rss reader display script?
Here's a piece of code that I use to display RSS feeds - it also makes use of Cache_Lite to cache the feeds so they are not queried at each request. It initially used XML_RSS, which ceased to work after the switch to PHP5. Then I changed just a few lines to make it use SimpleXML - now it works like a charm. CU Markus <BEGIN CODE> function printFeed($url, $headline=null, $maxEntries=10, $cacheHours=1, $useDesc=false, $printStruct=false) { // Set Cache_Lite options. $options = array( 'cacheDir' => './tmp/', 'lifeTime' => (int) (3600 * $cacheHours) ); $Cache_Lite = new Cache_Lite($options); // Define unique Cache-ID for this feed. $cacheID = $headline.$url.$maxEntries; // Let's see if there's valid cache data available for this feed if ($data = $Cache_Lite->get($cacheID)) {
	    // Indeed there is, so let's just print it....
	    echo $data;
} else {
	    $rss = @simplexml_load_file($url);
	    if (!$rss) {
	       return "<p class=\"headline\">Failed to open RSS feed for $title.</p>";
	    }
	    $image = '';
	    if (isset($rss->image) && !empty($rss->image)) {
$image = sprintf('<a href="%s"><img src="%s" alt="%s" title="%s" border="0" align="right" /></a>',
			                 $rss->image->link, $rss->image->url,
			                 $rss->image->title, $rss->image->title);
	    }
	    $data  = "<div class='newsfeed' height='100%'>";
	    $data .= sprintf ("<div class='headline'><a href='%s' title='%s' target='_blank'>%s</a></div>",
                             (isset($rss->channel->link) ? $rss->channel->link : $url),
                             (isset($rss->channel->description) ? htmlentities(utf8_decode($rss->channel->description)) : ''),
                             ($headline === null ? utf8_decode($rss->channel->title) : $headline));
            if (isset($rss->channel->item) && !empty($rss->channel->item)) {
                $news = $rss->channel->item;
            } elseif (isset($rss->item) && !empty($rss->item)) {
                $news = $rss->item;
            } else {
                $news = null;
            }
            $data .= "<div id='content'>";
            if ($news !== null) {
                $data .= "<ul>";
    	        for($i=0; $i < $maxEntries; $i++) {
                    if (isset($news[$i])) {
                        $title = htmlentities(utf8_decode($news[$i]->title));
                        if ($useDesc == true) {
                            $title = substr(htmlentities(utf8_decode($news[$i]->description)), 0, 20).'...';
                        }
                        $data .= "<li><a href='".$news[$i]->link."'>".$title."</a></li>\n";
                    }	
                }
                $data .= '</ul>';
            } else {
                $data .= "Dude, this feed is seriously f***ed up!";
                ob_start();
                echo '<pre>'; print_r($news); echo '</pre>';
                echo '<pre>'; print_r($rss); echo '</pre>';
                $debug = ob_get_contents();
                ob_end_clean();
                $data .= $debug;
            }
            if (!empty($image)) {
                $data .= $image.'<br>';
            }
	    $data .= '</div></div>';
  	    // Save feed into cache file...
            $Cache_Lite->save($data);
            // ...and then print it
            if ($printStruct == true) {
                echo "<pre>";
                print_r($news);
                echo "</pre>";
            } else {
                echo $data;
            }
        }
} </END CODE>

« previous php.pear.general (#15368) next »