WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-24-2007, 06:33 PM
spenland spenland is offline
WebProWorld Pro
 

Join Date: Jul 2004
Location: Irvine, CA
Posts: 120
spenland RepRank 0
Default Parsing RSS feed into PHP

Hi everyone,

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.mysite.com/rss_fixer_new.php';
$XMLpar = new SimpleXmlParser($rss_url);

?>

Heres the rss_fixer_new code:

<?php

$rss_url = 'http://www.mysite/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 (gobinathm at gmail dot com) */
/* */
/* 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
Reply With Quote
  #2 (permalink)  
Old 05-26-2007, 08:00 PM
Maloney Maloney is offline
WebProWorld Member
 

Join Date: Feb 2005
Posts: 72
Maloney RepRank 0
Default Re: Parsing RSS feed into PHP

I don't know, sorry, but I do use Carp myself and know that you can set the display count. There's a free version that does what you're looking for: CaRP : RSS to HTML converter : Free RSS PHP parser script download
__________________
Fabaroo.com Fabaroo Articles NiftyShops.com Fabarooni.com
*Currently Under Maintenance, No Submissions Just Yet
Reply With Quote
  #3 (permalink)  
Old 06-01-2007, 04:17 AM
le_gber le_gber is offline
WebProWorld Veteran
 

Join Date: Aug 2003
Location: Bedfordshire, UK
Posts: 340
le_gber RepRank 0
Default Re: Parsing RSS feed into PHP

You can use MagpieRSS and the little tutorial below (in french but you can translate it using google or babel fish)

La syndication de contenu : intérêt pour les webmasters

It took me 5 minutes to set up a feed display from the Beeb for my site
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: feed, parsing, php, rss



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
AUP for parsing SERP rucci Other Engines/Directories 2 03-14-2007 05:13 AM
Parsing Another Website With PHP webmasterjunkie Web Programming Discussion Forum 9 12-13-2005 08:29 PM
Meta parsing RFA MSN Search Discussion Forum 2 12-03-2004 10:14 AM
Work-around for php/sql image-parsing? DaButcher Search Engine Optimization Forum 0 08-20-2004 07:05 AM
New Google parsing Mel Google Discussion Forum 2 02-15-2004 02:10 PM


Search Engine Friendly URLs by vBSEO 3.0.0