百度新闻RSS的PHP应用

有时可能需要采集一些数据,而百度提供了RSS功能将很方便的被利用起来。

/**
 * 百度新闻订阅
 * @param string $rss_url:rss地址
 * @return type:返回数组title/pubdate/link
 */
function rss_baidu($rss_url) {
    header('Content-Type: text/html; charset=UTF-8');
//    $rss_url = 'http://news.baidu.com/n?cmd=7&loc=6692&name=%CB%C4%B4%A8&tn=rss';
    $rss_contents = file_get_contents($rss_url);
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $rss_contents, $values, $idx);
    xml_parser_free($parser);
    foreach ($values as $val) {
      $tag = $val["tag"];
      $type = $val["type"];
      $value = $val["value"];
      //标签统一转为小写
      $tag = strtolower($tag);
      if ($tag == "item" && $type == "open") {
        $is_item = 1;
      } else if ($tag == "item" && $type == "close") {
        //构造输出字符串
        $data[] = array(
            'title' => $title,
            'link' => $link,
//            'description' => $description,
            'pubdate' => $pubdate
        );
        $is_item = 0;
      }
      //仅读取item标签中的内容
      if ($is_item == 1) {
        $$tag = $value;
      }
    }
    return $data;
}