0

I'm creating a script to get posts from an rss feed and place them into my database. I'll do this with 3-5 different feeds and then I'll print them out in date order regardless of the feed they come from. Some of this works fine, like I can get the rss data from the file using simplepie but I can't seem to then add it to an array (trivial part).

The data from get_title() is coming back as a "SimplePie_Item" instead of just a string "Cannot use object of type SimplePie_Item as array". If I try to echo the data it prints the string out just fine. So I think there's something I don't get about data from objects here, like why I can't just copy the string in to the array. I tried casting but this didn't seem to do anything.

--update_database Method code--

function update_database($options=array())
{    
    //Check required fields.
    if(!$this->_required(array('feeds','life'),$options))
      return false;

    //Add default values
    $options = $this->_default(array() ,$options);

    if(is_array($options['feeds'])) //Multiple blogs
    {       

echo 'is an array';

        //Parse each url and add to db.
        foreach($options['feeds'] as $a => $u)
        {

echo 'Print r =

';print_r($u);echo '
';
echo 'feeds loop = '.$a;

echo 'url = '.$u['url'].'<br />';

$posts = $this->fetch_feed( array('url'=>$u['url']) );

            //Add to db.
            foreach($posts as $f)
            {   

  $add['post_title'] =    (string)$f->get_title();
  $add['link'] =          (string)$f->get_link();
  $add['p_description'] = (string)$f->get_description();
  $add['content'] =       (string)$f->get_content();
  $add['post_date'] =     (string)$f->get_date();
  $add['guid'] =          (string)$f->get_id();
  $add['status'] =        'active';
  $add['blog_id'] =       $u['blog_id'];

                $this->add_post($f);//Add posts to db.
            }
        }
    }
    else //Single blog.
    {
echo 'feeds - single feed';

$posts = $this->fetch_feed( array('url'=>$options['url']) );

            //Add to db.
            foreach($posts as $k=>$f)
            {                   
  $add['post_title'] =    (string)$f->get_title();
  $add['link'] =          (string)$f->get_link();
  $add['p_description'] = (string)$f->get_description();
  $add['content'] =       (string)$f->get_content();
  $add['post_date'] =     (string)$f->get_date();
  $add['guid'] =          (string)$f->get_id();
  $add['status'] =        'active';
  $add['blog_id'] =       $options['blog_id'];

                $this->add_post($f);//Add posts to db.
            }
    }
} 

--fetch_feed method--

  function fetch_feed($options=array())
  {
    $this->simplepie->set_feed_url($options['url']);
    $this->simplepie->set_cache_location(APPPATH.'cache/rss');
    $this->simplepie->init();
    $this->simplepie->handle_content_type();
    return $this->simplepie->get_items();
  }

--add_post method--

    function add_post($options)
    {
        //Check required options.
        if(!$this->_required(array('post_title','link','p_description','content','post_date','guid','status','blog_id'), $options))
          return false;

        //Add default values
        $options = $this->_default(array() ,$options);  

        $this->db->set('post_title',$options['title']);
        $this->db->set('link',$options['link']);
        $this->db->set('p_description',$options['description']);
$this->db->set('content',$options['content']);
$this->db->set('post_date',$options['post_date']);
$this->db->set('guid',$options['guid']);
$this->db->set('status',$options['status']);    
        $this->db->set('blog_id',$options['blog_id']);

        $this->db->insert('posts');
        return $this->db->affected_rows();
    }

--Controller--

  function simple_pie()
  {
    $this->load->model('post_model');
    //$this->options->feeds = $this->post_model->fetch_feed(array('url'=>'http://testigniter.blogspot.com/feeds/posts/default?alt=rss'));

    $urls = array('feeds'=>array( array('url'=>'http://testigniter.blogspot.com/feeds/posts/default?alt=rss','blog_id'=>1) ),'life'=>60);

    $this->options->result = $this->post_model->update_database($urls);

    if($this->options->result)
    {
      echo 'Passed';
    }
    else
    {
      echo 'Failed';
    }

    $this->load->view('pages/simplepie_test', $this->options);  
  }

No view is really needed here.

Adamantus
  • 813
  • 1
  • 12
  • 30
  • `$f->get_title()` should return string. What is your problem here? Please remove redundant code. – Shiplu Mokaddim Apr 12 '12 at 15:45
  • I have tested your code and the `$f->get_title();` method definitely returns a string. This is the var_dump of the `$f->get_title()`: `string 'Running Selenium 2 - WebDriver using Facebook / PHP-Webdriver Wrapper' (length=69)` – alxbrd Apr 12 '12 at 19:23
  • The error is on: $add['post_title'] = $f->get_title(); The error is: "Fatal error: Call to a member function get_title() on a non-object in application/models/post_model.php on line 71". P.s. See edit summary on original post. – Adamantus Apr 13 '12 at 13:06

2 Answers2

1

Echo converts a value through (string) before showing it.

Try prepend (string) before your fetched value, and you'll get the same result as the echo.

I believe your problem is that the value is actually array(0 => 'string') because this is one of the ways you can read and store XML structures, and when you echo it it looks good because:
(string)array(0 => 'string') == 'string'

Robin Castlin
  • 10,956
  • 1
  • 28
  • 44
  • I did already try casting a (string) to the title but I just got the same error but in my add_post() method instead. – Adamantus Apr 13 '12 at 14:37
0

PHP and I are not the best of friends, but if the problem is with SimplePie you should look at the flags you can set when you first declare your object, particularly enable_xml_dump(). This allows you to then get the raw XML from the feed and do whatever you want with it.

I've used that in the past to accomplish stuff. If you could somehow shorten your sample code/example that might help. Normally if you can fetch the feed, get_title() should just work... Bad feeds are often the culprit, are you sure the feed itself is fine and validates? There are RSS feed validators...

Muskie
  • 577
  • 3
  • 21