Jump to content

PHP RSS Reader


morrisjohnny

Recommended Posts

I've started to learn PHP and i've been improving my knowledge. I'm currently trying to create An RSS reader using PHP. I have tried searching the forums but i can't search for 'PHP RSS reader' as PHP and RSS are below for words.I'm using the current code below i found online but it shows the full page. I only want to retire the title and the link.

<?phpset_time_limit(0);$file = "http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/motorsport/formula_one/rss.xml";$rss_channel = array();$currently_writing = "";$main = "";$item_counter = 0;function startElement($parser, $name, $attrs) {   	global $rss_channel, $currently_writing, $main;   	switch($name) {   		case "RSS":   		case "RDF:RDF":   		case "ITEMS":   			$currently_writing = "";   			break;   		case "CHANNEL":   			$main = "CHANNEL";   			break;   		case "IMAGE":   			$main = "IMAGE";   			$rss_channel["IMAGE"] = array();   			break;   		case "ITEM":   			$main = "ITEMS";   			break;   		default:   			$currently_writing = $name;   			break;   	}}function endElement($parser, $name) {   	global $rss_channel, $currently_writing, $item_counter;   	$currently_writing = "";   	if ($name == "ITEM") {   		$item_counter++;   	}}function characterData($parser, $data) {	global $rss_channel, $currently_writing, $main, $item_counter;	if ($currently_writing != "") {		switch($main) {			case "CHANNEL":				if (isset($rss_channel[$currently_writing])) {					$rss_channel[$currently_writing] .= $data;				} else {					$rss_channel[$currently_writing] = $data;				}				break;			case "IMAGE":				if (isset($rss_channel[$main][$currently_writing])) {					$rss_channel[$main][$currently_writing] .= $data;				} else {					$rss_channel[$main][$currently_writing] = $data;				}				break;			case "ITEMS":				if (isset($rss_channel[$main][$item_counter][$currently_writing])) {					$rss_channel[$main][$item_counter][$currently_writing] .= $data;				} else {					//print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");					$rss_channel[$main][$item_counter][$currently_writing] = $data;				}				break;		}	}}$xml_parser = xml_parser_create();xml_set_element_handler($xml_parser, "startElement", "endElement");xml_set_character_data_handler($xml_parser, "characterData");if (!($fp = fopen($file, "r"))) {	die("could not open XML input");}while ($data = fread($fp, 4096)) {	if (!xml_parse($xml_parser, $data, feof($fp))) {		die(sprintf("XML error: %s at line %d",					xml_error_string(xml_get_error_code($xml_parser)),					xml_get_current_line_number($xml_parser)));	}}xml_parser_free($xml_parser);// output as HTMLprint ("<html><head><title>PHP RSS Reader</title></head><body>");if (isset($rss_channel["IMAGE"])) {	print ("<a href=\"" . $rss_channel["LINK"] . "\" target=\"_blank\"><img border=\"0\" src=\"" . $rss_channel["IMAGE"]["URL"] . "\" align=\"middle\" alt=\"" . $rss_channel["IMAGE"]["TITLE"] . "\"></a>  <font size=\"5\">" . $rss_channel["TITLE"] . "</font><br><br>");} else {	print ("<font size=\"5\">" . $rss_channel["TITLE"] . "</font><br><br>");}print ("<i>" . $rss_channel["DESCRIPTION"] . "</i><br><br>");if (isset($rss_channel["ITEMS"])) {	if (count($rss_channel["ITEMS"]) > 0) {		for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {			print ("\n<table width=\"100%\" border=\"1\"><tr><td width=\"100%\"><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\" target=\"_blank\"><h2>" . $rss_channel["ITEMS"][$i]["TITLE"] . "</h2></a></b>");			print ("<i>" . html_entity_decode($rss_channel["ITEMS"][$i]["DESCRIPTION"]) . "</i>");			print ("</td></tr></table><br>");		}	} else {		print ("<b>There are no articles in this feed.</b>");	}}print ("</body></html>");?>

I want to display a maxium of three titles and hyperlinks but i would like it layout out like this

<table><tr><td><a href='exteral_link_1'>Item1</a></td></tr><tr><td><a href='exteral_link_2'>Item2</a></td></tr><tr><td><a href='exteral_link_3'>Item3</a></td></tr></table>

Link to comment
Share on other sites

I can't help saying it, so I'll just say it. Use XSLT for that. It's a lot easier to read and tweak, and the best part is it can run in conjuction with any S3L, including PHP.Look at this post. You can see how little the PHP code is. Of course, the XSLT is big, but it's written in declarive fashion and in pure markup. There are only a few tweaks needed to make this work with RSS 2.0 and a few more to make it the layout you want.The small tweaks for RSS are the line:

<xsl:when test="*[name()=$pagedElement]">

that you must replace with:

<xsl:when test="channel/*[name()=$pagedElement]">

and also change

<xsl:for-each select="*[name()=$pagedElement and position()>=1+ $startPoint and position()<=$startPoint + $recordsPerPage]">

with:

<xsl:for-each select="channel/*[name()=$pagedElement and position()>=1+ $startPoint and position()<=$startPoint + $recordsPerPage]">

As for your layout... try it and then search through the XSLT. It won't be long until you understand the process.

Link to comment
Share on other sites

I got a code for reading RSS in a PHP file today:

<?// We make a new object$obj = new db();//Så kaller vi opp funksjonene våre$obj->koble_til();$obj->skrivut();$obj->steng_db();// CONNECTclass db{//Noen variablervar $hostname = "localhost";var $username = "your_username";var $password = "your_password";var $usertable = "your_table";var $dbName = "your_database_name";var $result;function koble_til(){// Connecting ...mysql_connect($this->hostname,$this->username,$this->password) || die(mysql_error());@mysql_select_db($this->dbName)  || die("Unable to select database");}function query($query){$data = array();$this->result = mysql_query($query);if(mysql_num_rows($this->result) != 0){ while($d = mysql_fetch_object($this->result)){  $data[] = $d; } return $data;}return null;}function skrivut(){$result = $this->query("SELECT * FROM $this->usertable ORDER BY id");if (!is_null($result)){ $now = date('d.m-Y@H:i:s', time()); $rss = <<<RSS <rss version="2.0">  <channel>\n  <title>Anders Moen :: News</title>  <description>Anders Moen's RSS feed</description>  <lastBuildDate>$now</lastBuildDate>  <link>http://andersmoen.hotserv.dk/new/</link>RSS;	 foreach($result as $r){	  $date = date('d/m/Y@H:i:s', $r->timestamp);	   $rss .=<<<RSS			<item>	<title>$r->title</title>	<pubDate>$r->date</pubDate> 	<description>$r->content</description>	<link>http://andersmoen.hotserv.dk/new/?page=view&id=$r->id</link>   </item>RSS; } $rss .="</channel></rss>";header('Content-type:text/xml');print $rss;}}function steng_db(){mysql_free_result($this->result);mysql_close();}}?>

Some of the explanations here is in Norwegian since I got it on a Norwegian forum today.Well, wanna see it? Click here

Link to comment
Share on other sites

That's not an RSS reader! It's MySQL to RSS generator. There's quite a difference.

Link to comment
Share on other sites

I d't think you have the idea. I'm trying to extra two bits of data given off from an RSS feed.using PHP i want to extract the title which is being given from a RSS feed ( then inset the hyperlink into the title)EG the RSS feed Gives off

<title>Nokia 3650 Combines Phone And Photos</title><description>I've been playing with the new Nokia 3650. Finally, someone has got the combination of a cell phone with digital camera capabilities right!</description><link>http://allgadgetsreviewed.com/nokia3650.html</link>

I want to turn that into this

<a href='http://allgadgetsreviewed.com/nokia3650.html'>Nokia 3650 Combines Phone And Photos</a>

But only show a maxium off three.

Link to comment
Share on other sites

You could use SimpleXML (http://php.net/manual/en/ref.simplexml.php) to extract the information from the structure.How exactly to do this is hard to tell without the complete structure (as it the part you gave us doesn't seam to be "alone", if it is so you need to add a root element).Here's an example using the info you gave (with a "unknown root", you don't need to know it's name using SimpleXML as this):

<?php$xml = new SimpleXML( $structure ); // Can parse a string or open a file, see the manualecho '<a href="'.$xml->link.'" target="_blank">'.$xml->title.'</a>';?>

to get the three first you can use a for-loop:

<?php$xml = new SimpleXML( $structure ); // Can parse a string or open a file, see the manualfor ($i = 0; $i < 3; $++) {	echo '<a href="'.$xml->post[$i]->link.'" target="_blank">'.$xml->post[$i]->title.'</a>';}?>

This will work if the structure looks something like this (But it'snt hard to adapt to another structure)

<root>  <post>	  <title>...</title>	  <description>...</description>	  <link>...</link>  </post>  <post>	  <title>...</title>	  <description>...</description>	  <link>...</link>  </post>  <post>.....</root>

Hope that helped.Good Luck and Don't Panic!

Link to comment
Share on other sites

You could use SimpleXML (http://php.net/manual/en/ref.simplexml.php) to extract the information from the structure....Good Luck and Don't Panic!
Okay Firstly Thanks. I Have no idea Wht i was ment to do. I showed the code i was using but it was just displaying the current page as it was. It was totaly wrong.I would be very greatful if someone could provide a full working code or even an address to google in which i could contact and more than certain get a reply since on their personalnised homepage it works perfect and this is where i got the idea from.I'm not using the code i posted in the first topic. I'm only wanting to extract the title and convert the title into a hyperlink while showing the titles text as the hyperlink. I hopesomeone can help me.Thanks for trying Mr_CHISOL
Link to comment
Share on other sites

Well if all that you want is a working PHP RSS Reader/Parser then you should have just searched on Google. Here are 2.http://www.phpfreaks.com/script/view/703.phphttp://lastrss.oslab.net/However, you should know that if you simply use these and don't even read through them and attempt to understand them then you are not learning anything and you will not increase your skill at coding PHP.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...