O'Reilly logo

PHP Hacks by Jack D. Herrington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required