Jump to content

RSS Parser - Need to add limit. HELP?


mightymaster

Recommended Posts

Currently using the following script on a site I am building for a local church. I need to know how to limit the number of entries shown. I only want it to pull like 5 - 10 entries. Right now it pulls them all I believe.

 

<?php
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$locations = array('http://spongecell.com/rss/events/TheOC/Upcoming+Events', 'http://spongecell.com/rss/events/TheOC/Upcoming+Events', 'http://spongecell.com/rss/events/TheOC/Upcoming+Events');
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") {
 echo "<dt><b><a href='upcoming_events.php' target='new'>" . trim($title) . "</a></b></dt>\n";
 echo "<br><br>\n";
 //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");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $locations[$random_key]);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);

//$fp = fopen($locations[$random_key], 'r') or die("Error reading RSS data.");
// while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data) 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);
?>

 

It also would be great to make the script list the full date. Right now is just lists "5/3". Would love it to list 05/03/09 or Sunday May 3rd. Ideas?

Link to comment
https://forums.phpfreaks.com/topic/156288-rss-parser-need-to-add-limit-help/
Share on other sites

You can not limit the amount of rss feeds read if using xml_*() functions. If you want to limit the amount of rss feeds read you need to first read the rss then loop over the entries and after each loop see if the limit has been reached.

 

<?php
// read the file:
$rssFeed = file_get_contents('../rss/feed.xml');
$xml = new SimpleXmlElement($rssFeed);
$counter = 0;
$limit = 10;
foreach ($xml as ..)
    ..
    $counter++;
    if ($counter === $limit) break;
}
?>

 

However there must be a way to only read the first x records instead of each and every entry

Ok ... That means what? LOL Sorry, relatively new to php.

 

What I need is the parser to read the date,time, and title of the feed and publish it. With that I would like to limit the number of entries to 5-10. Any other script recommendations then?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.