<?php $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; $locations = array('http://michaelthompson.org/news/goo-world.xml'); srand((float) microtime() * 10000000); // seed the random gen $random_key = array_rand($locations); function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { $tag = $name; } elseif ($name == "ITEM") { $insideitem = true; } } function endElement($parser, $name) { global $insideitem, $tag, $title, $description, $link; if ($name == "ITEM") { printf("<dt><b><a href='%s' target=new>%s</a></b></dt>", trim($link),htmlspecialchars(trim($title))); printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description))); $title = ""; $description = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { switch ($tag) { case "TITLE": $title .= $data; break; case "DESCRIPTION": $description .= $data; break; case "LINK": $link .= $data; break; } } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen($locations[$random_key], 'r') or die("Error reading RSS data."); $int = 0; while ($int != [b]n[/b]){ [b]$data = fread($fp, 1024);[/b] 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))); $int++; } fclose($fp); xml_parser_free($xml_parser); ?> Code (markup): n in while loop number to be changed. Here's a quick fix or something. n= 1 --- 1 item displayed n= 2 && fread($data, 256*3) --- 2 items displayed n= 2 --- 3 items displayed n= 3 --- 5 items displayed n= 4 --- 7 items displayed Not really a php expert, but to get the desired number of feeds you will needs to play around with 2 numbers: N and the number in fread just under N number in fread can be a multiple of 512, 1024... etc. Play around with them until the desired number of items is displayed
If you dont want the links you can further edit $data and remove <link></link> if you know php good enough and then just echo the data.
also, those numbers i tested only with the feed included in the code. If you use another feed you will probably have to pick new numbers to achieve the same amount of items displayed.
OK Nice to be able to contribute to this Made a small addition in BOLD that will add the new 'no follow' attribute to all the links to the news items. This means you will not pass your PR to people like CNN who really don't need it! <?php $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; $locations = array('http://michaelthompson.org/news/goo-world.xml'); srand((float) microtime() * 10000000); // seed the random gen $random_key = array_rand($locations); function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { $tag = $name; } elseif ($name == "ITEM") { $insideitem = true; } } function endElement($parser, $name) { global $insideitem, $tag, $title, $description, $link; if ($name == "ITEM") { printf("<dt><b><a href='%s' [B]target='new' rel='nofollow'[/B]>%s</a></b></dt>", trim($link),htmlspecialchars(trim($title))); printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description))); $title = ""; $description = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { switch ($tag) { case "TITLE": $title .= $data; break; case "DESCRIPTION": $description .= $data; break; case "LINK": $link .= $data; break; } } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen($locations[$random_key], 'r') or die("Error reading RSS data."); $int = 0; while ($int != n){ $data = fread($fp, 1024); 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))); $int++; } fclose($fp); xml_parser_free($xml_parser); ?> Code (markup): Also I am getting this XML error: junk after document element at line 1 Code (markup): Turning up at the bottom of some of my feeds and it is slowing the page loading a LOT Anyone know how to get rid? Thanks
If you don't want to mess with any programming and want to go from an RSS feed to html I recommend RSS Digest: http://www.bigbold.com/rssdigest/ If you just want to copy and paste some PHP, try out this script: http://www.feedforall.com/free-php-script.htm
Why would they use your script and give you a link when there is a perfectly good one here with no strings attached?? You are urging people to not use the one provided totally free here basically by calling it programming when you are giving a link to essentially the same code.
Thanks for the usefull addition. I get the same XML error. The error goes away when I decrease "n" sufficiently. saintdw I know next to nothing about PHP, but it looks like you get XML error whenever you try to display more items than there are available for the feed(e.g. n is too high). Your addition needs something to to exit the loop in order to complete it.
Cant say im a php expert my self. My addition is focused on limiting the number of items displayed havent tested it with large n numbers. If you want everything displayed then the original works great
Does this code cache the feed or hit the feed everytime someone displays a page using this code? You should really read this... http://www.kbcafe.com/rss/rssfeedstate.html There are alot of guidlines to help keep your app from being a "problem" reader you may want to consider.
I was able to get this started on our site as well - thanks for the code. For one feed I pulled, I got the following error: XML error: not well-formed (invalid token) at line 41 This was the code from line 41: $link .= $data; PHP: This error did not show up with the original two feeds listed in this thread. Any idea how I might go about straightening this out? Could this be a problem on the publisher's end? (BTW, if there were a step before 'newbie' in php coding, I'd be there - so please go easy on me). Hmmm....discovered something else - presented on it's own page, it seems to work fine, other than the error noted above. But pasted into an existing php page, and it takes over, and no other content on the page is viewable. I'm certain that I'm goofing it up on my end, just not sure how to go about figuring out how. Any help is appreciated. And on a funny note - went to pull a feed from a certain site, and sure enough, the feed included an article I had written for another site.
Are you sure the error is on line 41 of your code? (As opposed to line 41 in an included class file?) Just a thought. I'm late to this thread.
Works very well! Thanks to all that contributed to this RSS thread, it's helped me loads. Now to find a darn feed that is even remotely related to inkjet cartridges, printers and toner that isn't a blatant advertisement of one of my opposition. edit* And it does break the heck out of html validation too! Would this cause any of the search engines to ignore the sites? I've added one to a couple of places to my site http://www.outofinkandtoner.com.au and would hate for Google (whenever the heck I get out of the sandbox) to penalise me for "bad code"!
nevetS - It is entirely possible that the error is not on my side. Unfortunately, I don't have the knowledge to troubleshoot the issue.
Usuallly, php will report the error: Error in file.php line 41. Keep in mind that line 41 to you isn't necessarily line 41 to php. Usually what I do when the error is not obvious, is copy my original file over to a backup, then try to strip some earlier code out of the equation so I can narrow down the problem. Another method is to put a couple of <? echo "I got here without a problem";?> lines. I believe that when php counts lines, it counts them after the includes - so if there are 50 lines in your first include, then it would be in line 41 of that file. I'm not very sure of myself right now (just woke up), but it's at least something to look into.
Say, for example, you are using a world news feed, is there any way to save the data each time the feed updates, so as to be able to build up an archive from day to day ?
Did you end up getting some sort of answer to this question Padawan as I'm extremely interested in the answer too?