Part of the EllisLab Network
This thread is a discussion for the wiki article: RSSParser
   
2 of 3
2
RSSParser
Posted: 21 April 2010 09:02 PM   [ Ignore ]   [ # 16 ]  
Summer Student
Total Posts:  28
Joined  02-26-2010

I am having problems using this. I am trying to get yahoo news feeds. I get the following error.

A PHP Error was encountered

Severity
Warning

Message
SimpleXMLElement::__construct(): Entityline 23parser error Input is not proper UTF-8indicate encoding Bytes0xA0 0x6D 0x65 0x6E

Filename
libraries/rssparser.php

Line Number
90 

the link i am trying to parse is UTF-8 ...

http://news.search.yahoo.com/news/rss?p=home+intrusion&ei=UTF-8&fl=0&x=wrt

 

what is weird is that this one works fine.

http://news.search.yahoo.com/news/rss?p=business+theft&ei=UTF-8&fl=0&x=wrt

any suggestions?

Profile
 
 
Posted: 12 May 2010 06:05 PM   [ Ignore ]   [ # 17 ]  
Summer Student
Total Posts:  2
Joined  03-25-2010

port22 what was your solution?

Thanks.

Profile
 
 
Posted: 15 September 2010 09:48 AM   [ Ignore ]   [ # 18 ]  
Summer Student
Total Posts:  3
Joined  09-15-2010

I’m using RSSParser to pull in my own Wordpress(.org) blog feed into another site of mine.  I’ve modified the library a bit in order to pull in the full post content along with the excerpt (by default, RSSParser will only pull in the excerpt, by taking $item->description):

This block of code:

//Build the item array
      
foreach ($xml->channel->item as $item)
      
{
           $data 
= array();
           
$data['title'$item->title;
           
$data['description'$item->description;
           
$data['pubDate'$item->pubDate;
           
$data['link'$item->link;
           
$this->data[] $data;
      

Was modified to this block of code:

//Build the item array
      
foreach ($xml->channel->item as $item)
      
{
           $data 
= array();
           
$data['title'$item->title;
           
$data['description'$item->description;
           
$content $item->children('http://purl.org/rss/1.0/modules/content/');
           
$data['content'= (string)trim($content->encoded);
           
$data['pubDate'$item->pubDate;
           
$data['link'$item->link;
           
$this->data[] $data;
      

I learned about how to do this from the post Using SimpleXML To Parse RSS Feeds, so if you’d like to learn more about the two lines I added you should check out that post.

Profile
 
 
Posted: 23 March 2011 10:10 PM   [ Ignore ]   [ # 19 ]  
Summer Student
Total Posts:  6
Joined  03-21-2011

I’m having an issue in my environment (Ci2/MAMP) where I get an error when I am trying to load the RSSParser.

I use the below code:

//Load the shiny new rssparse
  
$this->load->library('RSSParser', array('url' => 'http://link.to/rss.xml''life' => 2));
  
//Get six items from the feed
  
$data $this->rssparser->getFeed(6);

  foreach (
$data as $item) :
    
// do stuff with $item['title'], $item['description'], etc.
  
endforeach; 

And I get this error:

SeverityNotice

Message
Undefined propertyCI_Loader::$rssparser

Filename
theme/content.php

Line Number
28 

Has anyone come across this/know how to fix it?

Profile
 
 
Posted: 24 March 2011 08:36 AM   [ Ignore ]   [ # 20 ]  
Summer Student
Total Posts:  3
Joined  09-15-2010

nnash - I hope this is helpful (I’m not totally certain it will be)...

I have the load code ($this->load->library) in my controller.  The error you provide displays your code is in “theme/content.php”.  I’m not sure if that matters.

I get items from the feed differently. I’m also not certain this matters. You use:
$data = $this->rssparser->getFeed(6);

I use:
$data = array(‘posts’ => $this->rssparser->getFeed(6));


I hope this is useful, but like I said - I’m not sure it will be.  I’m just spotting differences…

Profile
 
 
Posted: 24 March 2011 01:21 PM   [ Ignore ]   [ # 21 ]  
Summer Student
Total Posts:  6
Joined  03-21-2011

I tried your suggestions but no dice… thanks anyway though.

Profile
 
 
Posted: 24 March 2011 01:29 PM   [ Ignore ]   [ # 22 ]  
Summer Student
Total Posts:  3
Joined  09-15-2010

I’m sorry to hear that, nnash…
The only other thing that I can think of is maybe the RSSParser.php file isn’t in the application\libraries folder.

Profile
 
 
Posted: 24 March 2011 03:22 PM   [ Ignore ]   [ # 23 ]  
Summer Student
Total Posts:  6
Joined  03-21-2011

I had it in system\libraries, but after I moved it to application\libraries I still got the same error.

Edit: I was able to fix my issues by placing the RSSParser snippet in the index(); of my controller.

Profile
 
 
Posted: 19 May 2011 06:29 PM   [ Ignore ]   [ # 24 ]  
Summer Student
Total Posts:  2
Joined  05-19-2011

Hey Summer Student.

I had the same error as you.  Can you share the fix?

Thanks.

Profile
 
 
Posted: 25 May 2011 07:41 AM   [ Ignore ]   [ # 25 ]  
Summer Student
Total Posts:  11
Joined  05-27-2008

If you don’t require caching or if caching is giving you a headache you can try using the modified library below which is basically the same as the original but with all caching references removed.

Also changed the item array to return strings and not xml objects.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * This class is written based entirely on the work found below
 * www.techbytes.co.in/blogs/2006/01/15/consuming-rss-with-php-the-simple-way/
 * All credit should be given to the original author
*/

class RSSParser
{
    
// ===================//
    //Instance vars       //
    // ===================//

    /* Feed URI */
    
var $feed_uri;

    
/* Associative array containing all the feed items */
    
var $data;

    
/* Store RSS Channel Data in an array */
    
var $channel_data;

    
/*  Boolean variable which indicates whether an RSS feed was unavailable */
    
var $feed_unavailable;

    
// ================ //
    // Constructor      //
    // ================ //
    
function RSSParser($params{
          $this
->CI =& get_instance();
    
          
$this->feed_uri $params['url'];
          
$this->current_feed["title"'';
          
$this->current_feed["description"'';
          
$this->current_feed["link"'';
          
$this->data = array();
          
$this->channel_data = array();
    
          
//Attempt to parse the feed
          
$this->parse();
    
}

    
// =============== //
    // Methods         //
    // =============== //
    
function parse() {

      
//Parse the document
      
$rawFeed file_get_contents($this->feed_uri);
      
$xml = new SimpleXmlElement($rawFeed);

      
//Assign the channel data
      
$this->channel_data['title'= (string) $xml->channel->title;
      
$this->channel_data['description'= (string) $xml->channel->description;

      
//Build the item array
      
foreach ($xml->channel->item as $item)
      
{
           $data 
= array();
           
$data['title'= (string) $item->title;
           
$data['description'= (string) $item->description;
           
$data['pubDate'= (string) $item->pubDate;
           
$data['link'= (string) $item->link;
           
$this->data[] $data;
      
}

  }

    
/* Return the feeds one at a time: when there are no more feeds return false
     * @param No of items to return from the feed
     * @return Associative array of items
    */
    
function getFeed($num{
        $c 
0;
        
$return = array();
        foreach(
$this->data AS $item)
        
{
            $return[] 
$item;
            
$c++;
            if(
$c == $num) break;
        
}
        
return $return;
    
}

    
/* Return channel data for the feed */
    
function & getChannelData() {
        $flag 
false;
         if(!empty(
$this->channel_data)) {
            
return $this->channel_data;
        
else {
            
return $flag;
        
}
    }

    
/* Were we unable to retreive the feeds ?  */
    
function errorInResponse() {
       
return $this->feed_unavailable;
    
}

Profile
 
 
Posted: 01 June 2011 07:21 AM   [ Ignore ]   [ # 26 ]  
Summer Student
Total Posts:  2
Joined  06-01-2011

Parser is Work. Thnks!

 Signature 

В рунете появился новый отличный ресурс Маркет-ДВ http://www.market-dv.ru/ , где любой желающий может продать что-либо или найти товар по-вкусу, подобрать одежду или автомобиль, найти или предложить работу, завести питомца и многое другое.

Profile
 
 
Posted: 13 August 2011 03:33 PM   [ Ignore ]   [ # 27 ]  
Summer Student
Total Posts:  11
Joined  06-14-2010

Does anyone know how this measures up to SimplePie?

Profile
 
 
Posted: 28 October 2011 12:45 PM   [ Ignore ]   [ # 28 ]  
Grad Student
Avatar
Rank
Total Posts:  68
Joined  08-11-2008

I’ve slightly improved and ‘tidied’ up the library - https://github.com/ollierattue/codeigniter-rssparser

 Signature 

FormIgniter - Easy form generator for the CodeIgniter framework
Too many tabs - My weblog about web app development, startups, productivity and life design
Barometer - The easiest way to add a stylish feedback form to your website.
My Github code - Sage Pay, Autoresponder, EPDQ libraries
Follow me on Twitter

Profile
 
 
Posted: 06 March 2012 08:22 AM   [ Ignore ]   [ # 29 ]  
Grad Student
Rank
Total Posts:  50
Joined  10-14-2008

I have added some functions:

- clear function for recursive parser calls
- feed author (creator) field added to the parsed data

function parse()
 
{
  
// Are we caching?
  
if ($this->cache_life != 0)
  
{
   $this
->CI =& get_instance();
      
$cache_dir $this->CI->config->item('cache_path');
   
$filename $cache_dir.'rss_Parse_'.md5($this->feed_uri);

   
// Is there a cache file ?
   
if (file_exists($filename))
   
{
    
// Has it expired?
    
$timedif = (time() - filemtime($filename));

    if (
$timedif < ( $this->cache_life 60))
    
{
     $rawFeed 
file_get_contents($filename);
    
}
    
else
    
{
     
// So raise the falg
     
$this->write_cache_flag true;
    
}
   }
   
else
   
{
    
// Raise the flag to write the cache
    
$this->write_cache_flag true;
   
}
  }

  
// Reset
  
$this->current_feed['title''';
  
$this->current_feed['description''';
  
$this->current_feed['link''';
  
$this->data = array();
  
$this->channel_data = array();

  
// Parse the document
  
if (!isset($rawFeed))
  
{
   $rawFeed 
file_get_contents($this->feed_uri);
  
}

  $xml 
= new SimpleXmlElement($rawFeed);

  if (
$xml->channel)
  {
   
// Assign the channel data
   
$this->channel_data['title'$xml->channel->title;
   
$this->channel_data['description'$xml->channel->description;

   
// Build the item array
   
foreach ($xml->channel->item as $item)
   {
    $data 
= array();
    
$data['title'= (string)$item->title;
    
$data['description'= (string)$item->description;
    
$data['pubDate'= (string)$item->pubDate;
    
$data['link'= (string)$item->link;
    
$dc $item->children('http://purl.org/dc/elements/1.1/');
    
$data['author'= (string)$dc->creator;
    if (
$this->callback)
    
{
     $data 
call_user_func($this->callback$data$item);
    
}
    
    $this
->data[] $data;
   
}
  }
  
else
  
{
   
// Assign the channel data
   
$this->channel_data['title'$xml->title;
   
$this->channel_data['description'$xml->subtitle;

   
// Build the item array
   
foreach ($xml->entry as $item)
   
{
    $data 
= array();
    
$data['id'= (string)$item->id;
    
$data['title'= (string)$item->title;
    
$data['description'= (string)$item->content;
    
$data['pubDate'= (string)$item->published;
    
$data['link'= (string)$item->link['href'];
    
$dc $item->children('http://purl.org/dc/elements/1.1/');
    
$data['author'= (string)$dc->creator;
    
    if (
$this->callback)
    
{
     $data 
call_user_func($this->callback$data$item);
    
}
     
    $this
->data[] $data;
   
}
  }

  
// Do we need to write the cache file?
  
if ($this->write_cache_flag)
  
{
   
if (!$fp = @fopen($filename'wb'))
   {
    
echo "RSSParser error";
    
log_message('error'"Unable to write cache file: ".$filename);
    return;
   
}

   flock
($fpLOCK_EX);
   
fwrite($fp$rawFeed);
   
flock($fpLOCK_UN);
   
fclose($fp);
  
}

  
return TRUE;
 
}

/* Initialize the feed data */ 
 
function clear()
 
{
  $this
->feed_uri  NULL;
  
$this->data   FALSE;
  
$this->channel_data = array();
  
$this->cache_life 0;
  
$this->callback  FALSE;
  
  return 
$this;
 
}
Profile
 
 
Posted: 06 March 2012 08:40 AM   [ Ignore ]   [ # 30 ]  
Grad Student
Avatar
Rank
Total Posts:  68
Joined  08-11-2008

Thanks Stoney. Please submit this as a pull request on github and I will review and integrate - https://github.com/ollierattue/codeigniter-rssparser

 Signature 

FormIgniter - Easy form generator for the CodeIgniter framework
Too many tabs - My weblog about web app development, startups, productivity and life design
Barometer - The easiest way to add a stylish feedback form to your website.
My Github code - Sage Pay, Autoresponder, EPDQ libraries
Follow me on Twitter

Profile
 
 
   
2 of 3
2
 
‹‹ Quick n Simple Query Browser      Hello ››