[PHP] RSS Reading...

Soldato
Joined
12 Jun 2005
Posts
5,361
Hi there,

I am creating a little RSS reader. I don't have any PHP space at the moment so I am doing it locally so I will just show you the script.

I want the RSS to be shown in an image and I am using the following script, but whenever it is exected it just shows the image with no text.

What could be the problem?

Code:
<?php
class RSSParser {

	var $insideitem = false;
	var $tag = "";
	var $title = "";

	function startElement($parser, $tagName, $attrs) {
		if ($this->insideitem) {
			$this->tag = $tagName;
		} elseif ($tagName == "ITEM") {
			$this->insideitem = true;
		}
	}

	function endElement($parser, $tagName) {
		if ($tagName == "ITEM") {
			$slogan = vsprintf("%s",(trim($this->title)));
			$this->title = "";
			$this->insideitem = false;
		}
	}

	function characterData($parser, $data) {
		if ($this->insideitem) {
		switch ($this->tag) {
			case "TITLE":
			$this->title .= $data;
			break;
			case "DESCRIPTION":
			$this->description .= $data;
			break;
			case "LINK":
			$this->link .= $data;
			break;
		}
		}
	}
}


$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://ws.audioscrobbler.com/1.0/user/Cool_Guy/recenttracks.rss","r")
	or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
	xml_parse($xml_parser, $data, feof($fp))
		or die(sprintf("XML error: %s at line %d", 
			xml_error_string(xml_get_error_code($xml_parser)), 
			xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

$im = imagecreatetruecolor(400, 100);

$white = imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 0, 0, 147);
$font = "VERDANA.TTF";

imagefill($im, 0, 0, $white);

imagerectangle($im, 0, 0, 399, 99, $blue);

imagettftext($im, 8, 0, 5, 15, $blue, $font, $slogan);

header("Content-Type: image/png");
imagepng($im);
imagedestroy($im);

?>

Thanks...


[EDIT]

Just had a brainwave, I think I should write them to a text file first....
 
Last edited:
Merci Beacoup pour le link, but I have figured out what I have been doing wrong.....I have freed all the parse data yet I am still trying to use it after its freed, this is why I am getting nothing....?

Am I right?

I will have a look at the link now....
 
Hi there,

I have another quick PHP question.......I have no put an array in the class, how can I access the array outside the class?

Thanks...
 
a class is a template for an object, instantiating an object basically means putting the class template to use.

Code:
class Foo
{
    var bar;
}

$foo = new Foo; /* object is instantiated, and using the class template, this means $foo will have a member variable (property) of bar */

echo $foo->bar; /* we can now access this property */
 
also its generally good OO practice to not access class variables direct. Thats kind of the aim of the class , to encapsulated the functions and attributes relating of a specific object.

Usualy in other languages you would set the variables to private preventing you to access them direct but seen as php version 4 doesn't have this option it lets you get away with anything :)

its not needed but try accessing you class variables by setting up getter methods that return the variables value. That then gives you some control over how the variables are called and set. Plus it'll set you in good sted when/if you try other programming languages later on.
 
You set a timeout on the cache, naturally. Think about what you're doing there, accessing Audioscrobbler. How often, at the very minimum, will the data update? About 3 minutes or so, right, presuming that you're listening to songs all the time. So, you don't want to be fetching it more than once every 3 minutes or so, or else you'll hammer Last.fm's servers and not receive anything for your trouble.

Magpie implements caching natively, or you can just do something like:

Code:
$cache = './cache.txt';
$url = 'http://foo.bar/index.rss';

$rss = new rss($url);

if(!file_exists($cache)) {
    touch($cache);
    $rss->update();
    $rss->writeToCache();
}

if(filemtime($cache) + (3 * 60) > time())
{
    $rss->update();
    $rss->writeToCache();
}

$rss->readFromCache();
$data = $rss->parseXml();

foreach($data->items as $item)
{
    // whatever
}

...although your class should probably take care of all of that with private methods, and the external script should just have to call $rss->getUnserializedData() or somesuch.
 
Hi there,

I hear ya robmiller and I have tried to implement your suggestion with what seems success.

Here is the code for anyone that wants it:

Code:
<?php

$textfile = "tracks.txt";

if(filemtime($textfile) + (3 * 60) < time())
{

$fh1 = fopen($textfile, 'w+') or die("can't open file");
fwrite($fh1, "");
fclose($fh1);

class RSSParser {

	var $insideitem = false;
	var $tag = "";
	var $title = "";

	function startElement($parser, $tagName, $attrs) {
		if ($this->insideitem) {
			$this->tag = $tagName;
		} elseif ($tagName == "ITEM") {
			$this->insideitem = true;
		}
	}

	function endElement($parser, $tagName) {
		if ($tagName == "ITEM") {
			$textfile = "tracks.txt";
			$fh = fopen($textfile, 'a+') or die("can't open file");
			$tracks = sprintf("%s\n",(trim($this->title)));
			fwrite($fh, $tracks);
			fclose($fh);
			$this->title = "";
			$this->insideitem = false;
		}
	}

	function characterData($parser, $data) {
		if ($this->insideitem) {
		switch ($this->tag) {
			case "TITLE":
			$this->title .= $data;
			break;
			case "DESCRIPTION":
			$this->description .= $data;
			break;
			case "LINK":
			$this->link .= $data;
			break;
		}
		}
	}
}


$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://ws.audioscrobbler.com/1.0/user/Cool_Guy/recenttracks.rss","r")
	or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
	xml_parse($xml_parser, $data, feof($fp))
		or die(sprintf("XML error: %s at line %d", 
			xml_error_string(xml_get_error_code($xml_parser)), 
			xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

}

if ($slogans = @file("tracks.txt"))
{
$slogan = $slogans[0];
} else {
$slogan = "No Recent Music";
}

$im = imagecreatefrompng('background.png');

$blue = imagecolorallocate($im, 0, 0, 147);
$font = "font.ttf";

imagettftext($im, 10, 0, 112, 73, $blue, $font, $slogan);

header("Content-Type: image/png");
imagepng($im);
imagedestroy($im);


?>

Which gives me this:

untitled2wa.png


Thanks for all your help.

Thanks...
 
Back
Top Bottom