Hack90.Read RSS Feeds on Your PSP


Hack 90. Read RSS Feeds on Your PSP

Use PHP and the web browser in the PSP's 2.0 system software to read RSS feeds.

The Sony PlayStation Portable (PSP) was a great device even before it got the killer addition of a built-in web browser. But now, with this addition, the game-playing, web-surfing, hip-pocket-size device is simply indispensable.

This hack creates a specially formatted RSS feed data page that displays nicely on a single page of the PSP screen.

9.6.1. The Code

Save the code in Example 9-9 as index.php.

Example 9-9. An HTML RSS reader formatted for PSP
 <?php require_once( 'XML/RSS.php' ); function getValue( $node, $name ) { $nl = $node->getElementsByTagName( $name ); return $nl->item(0)->nodeValue; } $feeds = array( ); $feeds []= array( 'name' => 'Top Stories', 'url' => 'http://rss.cnn.com/rss/cnn_topstories.rss' ); $feeds []= array( 'name' => 'World', 'url' => 'http://rss.cnn.com/rss/cnn_world.rss' ); $feeds []= array( 'name' => 'U.S.', 'url' => 'http://rss.cnn.com/rss/cnn_us.rss' ); $feeds []= array( 'name' => 'Tech', 'url' => 'http://rss.cnn.com/rss/cnn_tech.rss' ); $feed = 0; if ( isset( $_GET['feed'] ) ) $feed = $_GET['feed']; ob_start( ); $ch = curl_init( ); curl_setopt($ch, CURLOPT_URL, $feeds[$feed]['url'] ); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); $rsstext = ob_get_clean( ); $cols = array( ); $cols []= ""; $cols []= ""; $cols []= ""; $col = 0; $doc = new DOMDocument( ); $doc->loadXML( $rsstext ); $il = $doc->getElementsByTagName( "item" ); for( $i = 0; $i < $il->length; $i++ ) { $item = $il->item( $i ); $title = getValue( $item, "title" ); $link = getValue( $item, "link" ); $description = getValue( $item, "description" ); $html = "<p class='story'><a href=\"$link\">$title</a></p>"; $cols[ $col ] .= $html; $col++; if ( $col >= 3 ) $col = 0; } ?> <html> <head> <title><?php echo( $feeds[$feed]['name'] ) ?></title> <style> body { margin: 0px; padding: 0px; } .link { font-weight: bold; margin-left: 10px; margin-right: 10px; } </style> </head> <body> <div style="width:478px;"> <div style="width:478px;border-bottom:1px solid black;margin-bottom: 5px;"> <?php $id = 0; foreach( $feeds as $f ) { ?> <a class='link' href="index.php?feed=<?php echo($id); ?>"> <?php echo( $f['name'] ); ?></a> <?php $id++; } ?> </div> <table> <tr> <td valign="top" width="33%"><?php echo( $cols[0] ); ?></td> <td valign="top" width="33%"><?php echo( $cols[1] ); ?></td> <td valign="top" width="33%"><?php echo( $cols[2] ); ?></td> </tr> </table> </div> </body> </html> 

The really interesting parts of this script are at the top, where the script finds the currently selected feed and downloads it using CURL. The script then uses the XML DOM system to parse up the RSS XML. Using getElementByTagName( ), it finds all of the item elements in the XML and stores their data into a set of column texts. Each column is specified by one entry in the $cols array.

The second half of the script outputs the list of possible RSS feeds, as well as the columns of article entries for the currently selected RSS feed, in an HTML format that fits nicely on a PSP screen.

9.6.2. Running the Hack

Upload the index.php file to the server and try the page first with Firefox. You should see something like Figure 9-10.

Figure 9-10. The RSS reader in Firefox


OK, that looks pretty good. Let's look at it on the PSP itself. Navigate to the URL by keying it into the address field. Then press the X button to fetch the URL. You should see something like Figure 9-11.

Figure 9-11. The RSS reader on the PSP


You can use the links along the top of the page to select the different categories of CNN news feeds. The links in the middle of the page go to the actual stories from the feed.

9.6.3. See Also

  • "Put Wikipedia on Your PSP" [Hack #99]

  • "Check Your Network Game with PHP" [Hack #98]



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net