PHP read RSS (feed) Simple instance _php instance

Source: Internet
Author: User
Tags xml parser

Recently in the blog, but a variety of blog synchronization between the trouble, fortunately, every blog has its own RSS aggregation system, can be implemented through the RSS blog synchronization call, so their own hands with the implementation of RSS blog to publish, in which the study of PHP read RSS.

RSS is written in XML, and XML is a data storage format. There are three ways to read XML data in PHP: XML parsing functions, DOM modules, and regular expressions are most directly parsing XML directly to get the data in XML.

The following is the parsing code:

Copy Code code as follows:

<?php
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 File
Fclose ($FP);

Create an XML parser
$parser = Xml_parser_create ();
Xml_parser_set_option--option settings for specified XML parsing
Xml_parser_set_option ($parser, xml_option_skip_white,1);
Xml_parse_into_struct-parsing XML data into an array $values
Xml_parse_into_struct ($parser, $buff, $values, $IDX);
Xml_parser_free--Frees the specified XML parser
Xml_parser_free ($parser);

foreach ($values as $val) {
$tag = $val ["tag"];
$type = $val ["type"];
$value = $val ["Value"];
Label unified to lowercase
$tag = Strtolower ($tag);

if ($tag = = "Item" && $type = = "Open") {
$is _item = 1;
}else if ($tag = = "Item" && $type = = "Close") {
Constructing an output string
echo "<a href=" ". $link." "target=" _blank ">". $title. " </a>
";
echo $content. "
";
$is _item = 0;
}
Read only the contents of 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 is the effect of reading the feed with this program:

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.