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.