how to read rss feeds using php

15
HOW TO READ RSS FEED USING PHP By makitweb

Upload: yogesh-singh

Post on 19-Jan-2017

873 views

Category:

Education


1 download

TRANSCRIPT

HOW TO READ RSS FEED USING PHP

By makitweb

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.

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.

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.

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

$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;

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; ?>

<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++; } }

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

Adding Some CSS

Over HTML Design

.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;}

.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;}

FinalOutput

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.