How to parse RSS in PHP

Source: Internet
Author: User
Tags foreach php code

This article mainly introduces the method of PHP parsing rss, example analysis of the PHP parsing RSS principle and XML file operation skills, need friends can refer to the

-->

This article describes the method of PHP parsing RSS. Share to everyone for your reference. Specifically as follows:

1. PHP code is as follows:

The code is as follows: <?php
Require "xml/rss.php";
$rss = new Xml_rss ("Http://php.net/news.rss");
$rss->parse ();
foreach ($rss->getitems () as $item) {
Print_r ($item);
}
?>
2. The rss.php code is as follows:
The code is as follows: <?php
$database = "Nameofthedatabase";
$dbconnect = Mysql_pconnect (localhost, dbuser, dbpassword);
mysql_select_db ($database, $dbconnect);
$query = "Select link, headline, description from ' headlines ' limit 15";
$result = mysql_query ($query, $dbconnect);
while ($line = Mysql_fetch_assoc ($result))
{
$return [] = $line;
}
$now = Date ("D, D M Y h:i:s T");
$output = "<?xml version=" 1.0 "?>
<rss version= "2.0" >
<channel>
<title>our Demo rss</title>
<link>http://www.tracypeterson.com/RSS/RSS.php</link>
<description>a Test rss</description>
<language>en-us</language>
<pubDate> $now </pubDate>
<lastBuildDate> $now </lastBuildDate>
<docs>http://someurl.com</docs>
<managingEditor>you@youremail.com</managingEditor>
<webMaster>you@youremail.com</webMaster>
";
foreach ($return as $line)
{
$output. = "<item><title>". Htmlentities ($line [' headline ']). " </title>
<link> ". Htmlentities ($line [' link '])." </link>
<description> ". Htmlentities (Strip_tags ($line [' description '])." </description>
</item> ";
}
$output. = "</channel></rss>";
Header ("Content-type:application/rss+xml");
Echo $output;
?>

I hope this article will help you with your PHP program design.

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.