笨。 RSS解析器将数据添加到db

时间:2012-04-12 15:32:55

标签: php oop codeigniter simplepie

我正在创建一个脚本来从rss Feed中获取帖子并将它们放入我的数据库中。我将使用3-5种不同的Feed进行此操作,然后我将按日期顺序打印出来,无论它们来自哪个Feed。其中一些工作正常,就像我可以使用simplepie从文件中获取rss数据但我似乎无法将其添加到数组(普通部分)。

来自get_title()的数据将作为“SimplePie_Item”返回,而不仅仅是字符串“不能使用SimplePie_Item类型的对象作为数组”。如果我尝试回显数据,它会打印出正确的字符串。所以我认为这里有一些关于对象数据的东西,比如为什么我不能将字符串复制到数组中。我尝试过铸造,但这似乎没有做任何事情。

- update_database方法代码 -

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'是一个数组';

        //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方法 -

  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方法 -

    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();
    }

- 控制器 -

  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);  
  }

这里真的不需要视图。

2 个答案:

答案 0 :(得分:1)

在显示之前,Echo将值转换为(string)

在获取值之前尝试前置(string),您将获得与回显相同的结果。

我认为您的问题是该值实际上是array(0 => 'string'),因为这是您可以读取和存储XML结构的方法之一,当您回显它时它看起来很好,因为:
(string)array(0 => 'string') == 'string'

答案 1 :(得分:0)

PHP和我不是最好的朋友,但如果问题出在SimplePie上,你应该看一下你第一次声明你的对象时可以设置的标志,特别是enable_xml_dump()。这样,您就可以从feed中获取原始XML,并随意使用它。

我过去用它来完成任务。如果你能以某种方式缩短可能有用的示例代码/示例。通常,如果你可以获取feed,get_title()应该正常工作......糟糕的提要通常是罪魁祸首,你确定提要本身是好的并且有效吗?有RSS feed验证器...

相关问题