Top Banner
HOW TO READ RSS FEED USING PHP By makitweb
15

How to read RSS feeds using PHP

Jan 19, 2017

Download

Education

Yogesh Singh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: How to read RSS feeds using PHP

HOW TO READ RSS FEED USING PHP

By makitweb

Page 2: How to read RSS feeds using PHP

Introduction:RSS(Rich Site Summary) is a format which is used in many websites which allow web publisher to syndicates their latest posts or data automatically.

Page 3: How to read RSS feeds using PHP

WHY:There is another method which allows the user to stay updated is bookmarking. But they need to manually go to websites on the timely basic and check what new have been added.

Page 4: How to read RSS feeds using PHP

Get StartedFor reading XML we are using

simplexml_load_file() function which

takes URL and return Object after

interpreting the XML.

Then we loop over Object for getting

content.

Page 5: How to read RSS feeds using PHP

Here, is complete code <html> <head> <meta charset="UTF-8"> <title>Reading rss feeds using PHP</title> <link href="style.css" type="text/css" rel="stylesheet"> </head> <body> <div class="content"> <form method="post" action=""> <input type="text" name="feedurl" placeholder="Enter website feed URL">&nbsp;<input type="submit" value="Submit" name="submit"> </form>

<?php

Page 6: How to read RSS feeds using PHP

$url = "http://makitweb.com/feed/"; if(isset($_POST['submit'])){ if($_POST['feedurl'] != ''){ $url = $_POST['feedurl']; } }

$invalidurl = false; if(@simplexml_load_file($url)){ $feeds = simplexml_load_file($url); }else{ $invalidurl = true; echo "<h2>Invalid RSS feed URL.</h2>"; }

$i=0;

Page 7: How to read RSS feeds using PHP

if(!empty($feeds)){

$site = $feeds->channel->title; $sitelink = $feeds->channel->link;

echo "<h1>".$site."</h1>"; foreach ($feeds->channel->item as $item) { $title = $item->title; $link = $item->link; $description = $item->description; $postDate = $item->pubDate; $pubDate = date('D, d M Y',strtotime($postDate));

if($i>=5) break; ?>

Page 8: How to read RSS feeds using PHP

<div class="post"> <div class="post-head"> <h2><a class="feed_title" href="<?php echo $link; ?>"><?php echo $title; ?></a></h2> <span><?php echo $pubDate; ?></span> </div> <div class="post-content"> <?php echo implode(' ', array_slice(explode(' ', $description), 0, 20)) . "..."; ?> <a href="<?php echo $link; ?>">Read more</a> </div> </div> <?php $i++; } }

Page 9: How to read RSS feeds using PHP

else{ if(!$invalidurl){ echo "<h2>No item found</h2>"; } } ?> </div> </body></html>

Page 10: How to read RSS feeds using PHP

Adding Some CSS

Over HTML Design

Page 11: How to read RSS feeds using PHP

.content{ width: 60%; margin: 0 auto;}

input[type=text]{ padding: 5px 10px; width: 60%; letter-spacing: 1px;}

h1{ border-bottom: 1px solid gray;}

input[type=submit]{ padding: 5px 15px; letter-spacing: 1px; border: 0; background: gold; color: white; font-weight: bold; font-size: 17px;}

h2{ color: black;}h2 a{ color: black; text-decoration: none;}

Page 12: How to read RSS feeds using PHP

.post{ border: 1px solid gray; padding: 5px; border-radius: 3px; margin-top: 15px;}

.post-head span{ font-size: 14px; color: gray; letter-spacing: 1px;}

.post-content{ font-size: 18px; color: black;}

Page 13: How to read RSS feeds using PHP

FinalOutput

Page 14: How to read RSS feeds using PHP

ConclusionWe hvae used PHP for reading RSS feeds of a website and created a page which shows recent 5 posts after reading the XML.

In HTML structure, we have also a textbox from where you can change feed URL.