Home > Backend Development > PHP Tutorial > rss feed code to read RSS feed using PHP

rss feed code to read RSS feed using PHP

WBOY
Release: 2016-07-29 08:38:32
Original
1053 people have browsed it

The RSS reading of Zhuyi.com is slightly modified based on this program. It mainly outputs an array string and solves the encoding problem.
php source code and detailed explanation of the code are as follows:
//RSS source address list array
$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");
//Set encoding to UTF-8
header('Content-Type:text/html;charset= UTF-8'); ($rssfeed);$i++){//Start decomposition
$buff = "";
$rss_str="";
//Open the rss address and read it, abort if the reading fails
$fp = fopen($ rssfeed[$i],"r") or die("can not open $rssfeed");
while ( !feof($fp) ) {
$buff .= fgets($fp,4096);
}
/ /Close the file to open
 fclose($fp);
  //Create an XML parser
  $parser = xml_parser_create();
  //xml_parser_set_option  --  Set options for the specified XML parsing      xml_parser_set_option($parser,X ML_OPTION_SKIP_WHITE,1) ;
//xml_parse_into_struct -- Parse XML data into the array $values ​​
xml_parse_into_struct($parser,$buff,$values,$idx);
//xml_parser_free -- Release the specified XML parser
$parser) ;
foreach ($values ​​as $val) {
$tag = $val["tag"];
$type = $val["type"];
$value = $val["value"];
// Tags are uniformly converted to lowercase $ $ tag = Strtolower ($ tag);
if ($ tag == "Item" && $ type == "open") {
$ is_item = 1;
} Else if ($ tag == "item" && $type == "close") {
                                                                                                                                                                                                                        . ;/a> if ($tag == "title ") {$title = $value;}
if ($tag == "link") {$link = $value;}
}
//Output result
echo $rss_str."
}
?>
The above has introduced the code for reading RSS feed using PHP, including the content of RSS feed. I hope it will be helpful to friends who are interested in PHP tutorials.


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template