Home  >  Article  >  Database  >  Code example for reading RSS feed using php

Code example for reading RSS feed using php

怪我咯
怪我咯Original
2017-07-11 16:33:542064browse

RSS (full name RDF Site Summary, Netscape's original definition), RSS is also a "webpage-like" description language (or document format), originally defined by Netscape (Netscape), RSS only has a relatively unified specifications (note that they are only specifications), the future is uncertain (the copyright issue of RSS 2.0). As a convenient interface for sharing website content, RSS was founded early, but it only became widely spread after the popularity of blogs (BLOG).

FEED (Chinese pronunciation of "fat" or "waste" is acceptable) is just an intermediate process, so no one in the world can give an accurate definition of FEED, so I still understand it from Tianyuan's point of view. You don’t need to worry about the definition of FEED. In fact, FEED is nothing. If I have to give an explanation, it would be better to understand it in an English environment. It seems more reasonable. FEED is actually the "middleman" between RSS (or ATOM) and subscribers, which plays a role in helping wholesale delivery of information. Therefore, the common formats of FEED are RSS and ATOM. The FEED subscription mentioned on the Internet should still be RSS or ATOM subscription to be more precise.

<?php 
//RSS源地址列表数组 
$rssfeed = array("http://www.jb51.net/feed", 
"http://rss.sina.com.cn/news/allnews/sports.xml", 
"http://ent.163.com/special/00031K7Q/rss_toutiao.xml", 
"http://tech.163.com/special/00091JPQ/techimportant.xml"); 

//设置编码为UTF-8 
header(&#39;Content-Type:text/html;charset= UTF-8&#39;);      

for($i=0;$i<sizeof($rssfeed);$i++){//分解开始 
    $buff = ""; 
    $rss_str=""; 
    //打开rss地址,并读取,读取失败则中止 
    $fp = fopen($rssfeed[$i],"r") or die("can not open $rssfeed");  
    while ( !feof($fp) ) { 
        $buff .= fgets($fp,4096); 
    } 
    //关闭文件打开 
    fclose($fp); 

    //建立一个 XML 解析器 
    $parser = xml_parser_create(); 
    //xml_parser_set_option -- 为指定 XML 解析进行选项设置 
    xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1); 
    //xml_parse_into_struct -- 将 XML 数据解析到数组$values中 
    xml_parse_into_struct($parser,$buff,$values,$idx); 
    //xml_parser_free -- 释放指定的 XML 解析器 
    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") { 
            //构造输出字符串 
            $rss_str .= "<a href=&#39;".$link."&#39; target=_blank>".$title."</a><br />"; 
            $is_item = 0; 
        } 
        //仅读取item标签中的内容 
        if($is_item==1){ 
            if ($tag == "title") {$title = $value;}         
            if ($tag == "link") {$link = $value;} 
        } 
    } 
    //输出结果 
    echo $rss_str."<br />"; 
} 
?>


The above is the detailed content of Code example for reading RSS feed using php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn