Home >Backend Development >PHP Tutorial >Code to read RSS feed with PHP_PHP tutorial
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');
for($i=0;$i
$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 Open file
fclose($fp);
//Create an XML parser
$parser = xml_parser_create();
//xml_parser_set_option -- Set options for specified XML parsing
xml_parser_set_option($parser,XML_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
xml_parser_free($parser);
foreach ($values as $val) {
$tag = $val["tag"];
$type 🎜>
if ($tag == "item" && $type == "open"){
$is_item = 1;
}else if ($tag == "item" && $type = = "CLOSE") {
// Construct output string
$ rss_str. = "& Lt; a href = '". $ Link. "' Target = _blank & gt;". " ;
";
$is_item = 0;
}
//Only read the content in the item tag
if($is_item==1){
if ($tag == "title") {$title = $value;}
if ($tag == "link") {$link = $value;}
}
}
/ /Output results
echo $rss_str."
";
}
?>
http://www.bkjia.com/PHPjc/319327.html