Forum Moderators: coopster
I'm redesigning a website and want to use an existing RSS parser that is used on the website, but I need to be able to limit the amount of feeds that it displays. Here is the code I use to call the parser:
<?
include_once("XmlNewParser.class.php");
$rss_url = 'http://www.example.com/rss_fixer_new.php';
$XMLpar = new SimpleXmlParser($rss_url);
?>
Heres the rss_fixer_new code:
<?php
$rss_url = 'http://www.example.com/blog/?feed=rss2';
header("Content-Type: text/xml");
echo shell_exec('wget -q -O - '.$rss_url);
?>
And here is the XMLNewParser.class.php code
<?php
/************************************************** **********************/
/* XmlParser.class.php */
/* ================================================== ===================*/
/* Copyright (c) 2005 by Gobinath */
/* */
/* Author(s): Gobinath */
/* ================================================== ===================*/
/* Title: A Simple XML parser Class */
/* Date: April 27th, 2005 */
/* ================================================== ===================*/
/* Credits:
I have completed this class based on the articles from php.net */
/* ================================================== ===================*/
/* Information: The Class does not Check the error nor validate the XML */
/* its works fine with a Well formed XML */
/* ================================================== ===================*/
class SimpleXmlParser {
public
# Variable Holding Parser
$SimpleParser = null,
$feedUrl = null,
# Variables to Hold the Data
$title = '',
$description = '',
$link = '',
$author = '',
$pubDate = '',
$insideitem = false,
$tag = '';
public function __construct($MyFeed) {
# To begin, I create an instance of the XML parser
# creates a new XML parser and returns a reousurce handle referencing
$this->SimpleParser = xml_parser_create();
# Assigns the Feed Source to the Member Variable
$this->feedUrl = $MyFeed;
# allows to use parser inside object
xml_set_object($this->SimpleParser, $this);
# Sets the element handler functions for the XML parser parser
xml_set_element_handler($this->SimpleParser, 'XmlParserFirstElement', 'XmlParserendElement');
# Sets the character data handler function for the XML parser parser
xml_set_character_data_handler($this->SimpleParser, 'characterData');
# Call to Parser Function
$this->ParseFeed();
}
public function SimpleXmlParser($MyFeed) {
# Purpose : Constructor, Which will initialize the XML Parser
$this->__construct($MyFeed);
}
public function XmlParserFirstElement($parser, $tagName, $attrs) {
# The Function Will be called, when ever the XML_PARSER Encounters a start Tag, in the XML File
if ($this->insideitem) {
$this->tag = $tagName;
} elseif (strtoupper($tagName)=='ITEM') {
$this->insideitem = true;
}
}
public function XmlParserendElement($parser, $tagName) {
# The Function Will be called, when ever the XML_PARSER Encounters a end Tag, in the XML File
if (strtoupper($tagName)=='ITEM') {
# pubDate element is made to display in HTML
printf("<div style='padding:5px;'><div class='news_date'>%s</div>", date("m.d.Y",strtotime($this->pubdate)));
# Description element is made to display in HTML
printf("<div ><a class='news_text' href='%s' target='new'>%s</a><br /></div><div style='clear:both'></div></div>", trim($this->link), htmlspecialchars(trim($this->title)));
# Deallocation of all Global Variables
$this->title = '';
$this->description = '';
$this->link = '';
$this->pubdate = '';
$this->insideitem = false;
}
}
# Function will be called by the Parser when the end tage of the element is processed. Requires Two Permeters
public function characterData($parser, $data) {
# Permeters: the parser instance and the string containing the data.
if ($this->insideitem) {
switch (strtoupper($this->tag)) {
case 'TITLE':
$this->title .= $data;
break;
case 'DESCRIPTION':
$this->description .= $data;
break;
case 'LINK':
$this->link .= $data;
break;
case 'PUBDATE':
$this->pubdate .= $data;
break;
}
}
}
public function ParseFeed(){
# This is the Place we need to Do error Handling for the XML file, which is not done in this class
# Open the XML file for reading.
# This part will be executed when we compiler is Unable to Open the XML Feed
$fp = fopen($this->feedUrl, 'r') or die('Oops! Unexpected Error While Reading the Feed');
# Starts reading the contents of the file, 4K at a time, and put it in the variable “$data”
while ($data = fread($fp, 4096)) {
xml_parse($this->SimpleParser, $data, feof($fp));
}
# Perform Some Clean Up Work Before Closing the File.
# Closing the XML File
fclose($fp);
# Release the XML Parser
xml_parser_free($this->SimpleParser);
}
}
?>
I would like to limit the parser to 5 feeds. How do I do this?
Thanks
[edited by: eelixduppy at 11:55 pm (utc) on May 25, 2007]
[edit reason] removed specifics [/edit]
I think changing this:
$fp = fopen($this->feedUrl, 'r') or die('Oops! Unexpected Error While Reading the Feed');
# Starts reading the contents of the file, 4K at a time, and put it in the variable “$data”
while ($data = fread($fp, 4096)) {
xml_parse($this->SimpleParser, $data, feof($fp));
}
# Perform Some Clean Up Work Before Closing the File.
# Closing the XML File
fclose($fp);
To this:
$fp = file($this->feedUrl);
for ($i=0; $i<6; $i++)
{
if ($i<5)
{
xml_parse($this->SimpleParser, $fp[$i]);
}
else
{
xml_parse($this->SimpleParser, $fp[$i],1);
}
}
Should work ok. Not tested though. Never worked with xml_parse before, but I checked the docs and I think it should be ok.
dc
Are you sure there was no error? Have you checked your error logs?
If you are running a dedicated server, the default location for the error log is in the server's error log, which is located in the apache directory (if using apache). This may have been changed. You can always try adding this to the top of the script:
error_reporting(E_ALL);
to see what you get. Report any errors that you get here with the corresponding lines of code.
<?php
/************************************************************************/
/* XmlParser.class.php */
/* =====================================================================*/
/* Copyright (c) 2005 by Gobinath */
/* */
/* Author(s): Gobinath */
/* =====================================================================*/
/* Title: A Simple XML parser Class */
/* Date: April 27th, 2005 */
/* =====================================================================*/
/* Credits:
I have completed this class based on the articles from php.net */
/* =====================================================================*/
/* Information: The Class does not Check the error nor validate the XML */
/* its works fine with a Well formed XML */
/* =====================================================================*/
class SimpleXmlParser {
public
# Variable Holding Parser
$SimpleParser = null,
$feedUrl = null,
# Variables to Hold the Data
$title = '',
$description = '',
$link = '',
$author = '',
$pubDate = '',
$insideitem = false,
$tag = '';
public function __construct($MyFeed) {
# To begin, I create an instance of the XML parser
# creates a new XML parser and returns a reousurce handle referencing
$this->SimpleParser = xml_parser_create();
# Assigns the Feed Source to the Member Variable
$this->feedUrl = $MyFeed;
# allows to use parser inside object
xml_set_object($this->SimpleParser, $this);
# Sets the element handler functions for the XML parser parser
xml_set_element_handler($this->SimpleParser, 'XmlParserFirstElement', 'XmlParserendElement');
# Sets the character data handler function for the XML parser parser
xml_set_character_data_handler($this->SimpleParser, 'characterData');
# Call to Parser Function
$this->ParseFeed();
}
public function SimpleXmlParser($MyFeed) {
# Purpose : Constructor, Which will initialize the XML Parser
$this->__construct($MyFeed);
}
public function XmlParserFirstElement($parser, $tagName, $attrs) {
# The Function Will be called, when ever the XML_PARSER Encounters a start Tag, in the XML File
if ($this->insideitem) {
$this->tag = $tagName;
} elseif (strtoupper($tagName)=='ITEM') {
$this->insideitem = true;
}
}
public function XmlParserendElement($parser, $tagName) {
# The Function Will be called, when ever the XML_PARSER Encounters a end Tag, in the XML File
if (strtoupper($tagName)=='ITEM') {
# pubDate element is made to display in HTML
printf("<div style='padding:5px;'><div class='news_date'>%s</div>", date("m.d.Y",strtotime($this->pubdate)));
# Description element is made to display in HTML
printf("<div ><a class='news_text' href='%s' target='new'>%s</a><br /></div><div style='clear:both'></div></div>", trim($this->link), htmlspecialchars(trim($this->title)));
# Deallocation of all Global Variables
$this->title = '';
$this->description = '';
$this->link = '';
$this->pubdate = '';
$this->insideitem = false;
}
}
# Function will be called by the Parser when the end tage of the element is processed. Requires Two Permeters
public function characterData($parser, $data) {
# Permeters: the parser instance and the string containing the data.
if ($this->insideitem) {
switch (strtoupper($this->tag)) {
case 'TITLE':
$this->title .= $data;
break;
case 'DESCRIPTION':
$this->description .= $data;
break;
case 'LINK':
$this->link .= $data;
break;
case 'PUBDATE':
$this->pubdate .= $data;
break;
}
}
}
public function ParseFeed()
{
$fp = file($this->feedUrl);
for ($i=0; $i<6; $i++)
{
if ($i<5)
{
xml_parse($this->SimpleParser, $fp[$i]);
}
else
{
xml_parse($this->SimpleParser, $fp[$i],1);
}
}
xml_parser_free($this->SimpleParser);
}
}
?>
[edited by: eelixduppy at 8:40 pm (utc) on May 30, 2007]
[edit reason] removed specifics [/edit]