Code for reading RSSfeed with PHP _ PHP Tutorial

Source: Internet
Author: User
Use PHP to read RSSfeed code. The help of the rss reading is slightly transformed on the basis of this program, mainly to output an array string and solve the encoding problem. Php source code and code are explained in detail as follows, and the rss reading of Yi net is slightly transformed on the basis of this program.
If an array string is output and the encoding problem is solved.

Php source code and code are described 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 the encoding to UTF-8
Header ('content-Type: text/html; charset = UTF-8 ');

For ($ I = 0; $ I $ Buff = "";
$ Rss_str = "";
// Open the rss address and read it. if reading fails, stop
$ Fp = fopen ($ rssfeed [$ I], "r") or die ("can not open $ rssfeed ");
While (! Feof ($ fp )){
$ Buff. = fgets ($ fp, 4096 );
}
// Close the 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_pai_struct -- parse XML data to 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 = $ val ["type"];
$ Value = $ val ["value"];
// Convert tags to lowercase
$ Tag = strtolower ($ tag );

If ($ tag = "item" & $ type = "open "){
$ Is_item = 1;
} Else if ($ tag = "item" & $ type = "close "){
// Construct the output string
$ Rss_str. = "". $ title ."
";
$ Is_item = 0;
}
// Read only the content in the item label
If ($ is_item = 1 ){
If ($ tag = "title") {$ title = $ value ;}
If ($ tag = "link") {$ link = $ value ;}
}
}
// Output result
Echo $ rss_str ."
";
}
?>

Parse outputs an array string and solves the encoding problem. Php source code and code are explained in detail as follows...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.