PHP: reading RSS (Feed) simple instance _ php instance

Source: Internet
Author: User
Tags xml parser
This article mainly describes how to use PHP to read RSS (Feed) instances. it is relatively simple. if you need it, refer to getting a blog recently. However, it is troublesome to synchronize various blogs, fortunately, every blog has its own RSS aggregation system, which can be used to implement synchronous blog calling. Therefore, we can use RSS to implement simultaneous blog publishing. here, we will take a look at reading RSS in PHP.

RSS is written in XML, and XML is a data storage format. There are three methods for PHP to read XML data: using XML parsing functions, DOM modules, and regular expressions, the most direct method is to directly parse XML and obtain the data in XML.

The following is the parsing code:

The code is as follows:


Error_reporting (E_ALL ^ E_NOTICE );

$ Rssfeed = "feed. xml ";
Header ('content-Type: text/html; charset = UTF-8 ');

$ Buff = "";
// Open the rss address and read
$ Fp = fopen ($ rssfeed, "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
Echo "". $ title ."
";
// Echo $ content ."
";
$ 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 ;}
If ($ tag = "content: encoded") {$ content = $ value ;}
}
}
?>

The following shows the effect of using this program to read the feed:

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.