Here is a script that you can use to read rss feeds, please take note this script don’t work to atom.xml.
If you want to learn more tweak about this script you can go to Digital point. This script was provided by ysf1 a senior member of DP.
You can modify or tweak this script to make it more powerful. Use the first script if you plan to use single rss feed only and the second code if you plan to rotate a feed.
To make this rss feed reader work, you need to replace the rss feed url with the source you want.
- <?php
- $insideitem = false;
- $tag = "";
- $title = "";
- $description = "";
- $link = "";
- 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’>%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("http://michaelthompson.org/news/goo-world.xml","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);
- ?>
-
复制代码
The second script
- <?php
- $insideitem = false;
- $tag = "";
- $title = "";
- $description = "";
- $link = "";
- $locations = array(’[url]http://michaelthompson.org/news/goo-world.xml[/url]’, ‘[url]http://forums.seochat.com/external.php[/url]’, ‘[url]http://michaelthompson.org/news/goo-world.xml[/url]’);
- 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.");
- 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);
- ?>
复制代码 |