Home >Backend Development >PHP Tutorial >rss feed code to read RSS feed using PHP
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.