Part of the EllisLab Network
This thread is a discussion for the wiki article: RSSParser
   
1 of 2
1
RSSParser
Posted: 13 September 2008 10:39 AM   [ Ignore ]  
Summer Student
Total Posts:  3
Joined  05-12-2008

Promising!  A couple things, though:

Line 40 is a typo:
&get;_instance();

should instead be

&get;_instance();

Also, $cache_path is undefined. 

Best,
M

Profile
 
 
Posted: 13 September 2008 11:20 AM   [ Ignore ]   [ # 1 ]  
Administrator
Avatar
RankRankRankRankRank
Total Posts:  3097
Joined  01-07-2008

I’ve fixed up the get_instance bug.
Always try to follow &s with a space or use the html entity.

I believe $cache_path should be $filename or $cache_dir, but I’ve never used the library so I’ll leave changing that to someone else.  It’s only a log message after all.

 Signature 
Profile
MSG
 
 
Posted: 13 September 2008 12:51 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  3
Joined  05-12-2008

Yeah, I’ve been using $filename, and the parser works.  I’ll probably end up modifying it to my purposes, so I can parse multiple feeds in the same page load.

Profile
 
 
Posted: 11 December 2008 05:46 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  1
Joined  05-29-2008

Change this :

//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'$item->title;
            
$data['description'$item->description;
            
$data['pubDate'$item->pubDate;
            
$data['link'$item->link;
            
$this->data[] $data;
        

Into this :

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'$item->title;
            
$data['description'$item->description;
            
$data['pubDate'$item->pubDate;
            
$data['link'$item->link;
            
$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['title'$item->title;
            
$data['description'$item->content;
            
$data['pubDate'$item->published;
            
$data['link'$item->link;
            
$this->data[] $data;
        
}
          
      } 

If you get an error because some blogs using different “kind” of RSS xml format (i don’t know what is it).

Maybe you can add this to the lib.

Profile
 
 
Posted: 13 December 2008 02:56 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  38
Joined  11-20-2007

Hi,
When i heat F5 to refresh the page I get a PHP Warning ERROR:

A PHP Error was encountered
Severity: Warning
Message: unserialize() [function.unserialize]: Node no longer exists
Filename: libraries/RSSParser.php
Line Number: 75

Why is happening this?

 Signature 

A.

Profile
 
 
Posted: 16 March 2009 03:07 PM   [ Ignore ]   [ # 5 ]  
Lab Assistant
RankRank
Total Posts:  225
Joined  06-04-2007

is working ?

 Signature 

http://www.asinox.net
CodeIgniter code and more.

Profile
 
 
Posted: 29 March 2009 05:23 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  8
Joined  03-16-2009
Asinox - 16 March 2009 07:07 PM

is working ?

No, I got the same error as the penguin above.

Profile
 
 
Posted: 29 March 2009 05:27 PM   [ Ignore ]   [ # 7 ]  
Lab Assistant
RankRank
Total Posts:  225
Joined  06-04-2007

ok, anyway im using another one

http://www.derekallard.com/blog/post/building-an-rss-feed-in-code-igniter/

 Signature 

http://www.asinox.net
CodeIgniter code and more.

Profile
 
 
Posted: 03 July 2009 04:00 PM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  2
Joined  05-09-2007

I’m also getting the serialize() warning, it is apparently that serialize() does not place nice with SimpleXML. Some comprehensive Googling led me to this PHP bug, which isn’t actually a bug, and this super helpful post on Make Me Pulse, which seems like a solution but it just made my head hurt.

My solution is a bit more pragmatic: don’t serialize the XML.

You can see my version here: http://codeigniter.com/wiki/RSSParser-PhilMod/

Profile
 
 
Posted: 18 August 2009 10:51 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  9
Joined  07-19-2008

Phil Nelson,


I am using your library, but i am with a problem.
the encode type utf-8, i tried:
strip_tags(utf8_encode(htmlspecialchars($item->title)));
strip_tags(htmlspecialchars($item->title));
nothing work.
thanks for help

Profile
 
 
Posted: 18 August 2009 10:55 PM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  9
Joined  07-19-2008

Resolved:
utf8_encode(htmlentities($item->title,ENT_COMPAT,‘utf-8’));

Profile
 
 
Posted: 20 August 2009 08:00 PM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  9
Joined  07-19-2008

Hi guys,

i hava a problem when use rssparser and parser class by CI:
A PHP Error was encountered

Severity: Warning

Message: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 2

Filename: libraries/Parser.php

Line Number: 162

in line 162 print |{0}(.+?){/0}|s that is passed to preg_match
Any Ideas?
tks

Profile
 
 
Posted: 12 January 2010 03:23 AM   [ Ignore ]   [ # 12 ]  
Grad Student
Avatar
Rank
Total Posts:  36
Joined  07-06-2009

The unserialize() bug is caused by the caching. Turn off caching (set life=>0 to fetching parameters) and plugin works OK.

 Signature 

Codito, ergo sum - I code, therefore I am

Profile
 
 
Posted: 28 January 2010 06:22 PM   [ Ignore ]   [ # 13 ]  
Summer Student
Total Posts:  19
Joined  02-05-2008

Just cast the SimpleXML objects to strings.

$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
Profile
 
 
Posted: 23 March 2010 07:10 AM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  9
Joined  03-04-2010

I’m having trouble getting this to work with the latest stable CI version of this posting.

I have modified the arrays to match the XML format being used:

<?xml version="1.0" encoding="UTF-8"?>

<entry>
   <
title type="html"></title>
   <
id></id>
   <
published></published>
   <
content type="html"></content>
</
entry

But CI gives me this error message:

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: libraries/RSSParser.php

Line Number: 100

Line Numbers: 99 & 100

//Build the item array
foreach ($xml->channel->item as $item

Any help or do you need more information? I have tried both an external RSS feed and just a simple XML file I made as shown above.

Here is the complete code with the modifications I made.

<?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;

    
/* Cache lifetime */
    
var $cache_life;

    
/* Flag to write to cache - defaulted to false*/
    
var $write_cache_flag false;

    
/* Code Ignitor cache directory */
    
var $cache_dir;

    
// ================ //
    // Constructor      //
    // ================ //
    
function RSSParser($params{
          $this
->CI =& get_instance();
          
$this->cache_dir = ($this->CI->config->item('cache_path') == '') ? BASEPATH.'cache/' $this->CI->config->item('cache_path');

          
//$this->cache_dir = '/system/cache';
          
$this->cache_life $params['life'];
    
          
$this->feed_uri $params['url'];
          
$this->current_feed["id"'';
          
$this->current_feed["title"'';
          
$this->current_feed["published"'';
          
$this->current_feed["content"'';
          
$this->data = array();
          
$this->channel_data = array();
    
          
//Attempt to parse the feed
          
$this->parse();
    
}

    
// =============== //
    // Methods         //
    // =============== //
    
function parse() {
        
//Are we caching?
        
if ($this->cache_life != 0)
        
{

            $filename 
$this->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))
                
{
                    
//its ok - so we can skip all the parsing and just return the cached array here
                    
$this->data unserialize(implode(''file($filename)));
                    return 
true;
                
}
               
//So raise the falg
               
$this->write_cache_flag true;

            
else {
               
//Raise the flag to write the cache
               
$this->write_cache_flag true;
            
}
       }


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

      
//Assign the channel data
      
$this->channel_data['id'$xml->channel->id;
      
$this->channel_data['title'$xml->channel->title;
      
$this->channel_data['published'$xml->channel->published;
      
$this->channel_data['content'$xml->channel->content;

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

      
//Do we need to write the cache file?
      
if ($this->write_cache_flag)
      
{
            
if ( ! $fp = @fopen($filename'wb'))
            
{
                
echo "ERROR";
                
log_message('error'"Unable to write cache file: ".$filename);
                return;
            
}
            flock
($fpLOCK_EX);
            
fwrite($fpserialize($this->data));
            
flock($fpLOCK_UN);
            
fclose($fp);
      
}
      
return true;
  
}

    
/* 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: 23 March 2010 12:44 PM   [ Ignore ]   [ # 15 ]  
Summer Student
Total Posts:  9
Joined  03-04-2010

I figured it out. Good to go.

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