Want some feeds? |
Step 1: Download MagpieRSS on sourceforge (I got magpierss-0.72) and place it in your web directory. I got the following files:
Step 2: Coding.
index.php
<html> <head> <title>Parsing RSS feeds with MagPie RSS</title> </head> <body> <?php require_once('rss_fetch.inc'); //Specify the URL of RSS feeds $rss = fetch_rss("http://rss.cnn.com/rss/cnn_topstories.rss"); foreach ( $rss->items as $item ) { //loop through the $rss array which contains the feeds echo "<div style='margin: 5px 0 5px 0;'>"; echo "<div style='font-weight: bold;'>{$item['title']}</div>"; echo "<div style=''>{$item['description']}</div>"; echo "</div>"; } ?> </body> </html>
If we will take a look at http://rss.cnn.com/rss/cnn_topstories.rss, we have:
On our index.php code, title and description inside $item variable are from the XML tags of RSS feed. If you will view-source http://rss.cnn.com/rss/cnn_topstories.rss, you will see:
Please Click to enlarge |
This is an item in our RSS feed. The values between title and description tags are echoed in our program. You can also echo the link, pubDate, etc. since those tags are there too.
Step 3: Run the program. The ouput of our code would be something like:
That's it! Now you can embed some contents from an RSS feeds to your website!
For FREE programming tutorials, click the red button below and subscribe! :)
website