In today’s fast-paced world, staying up to date with the latest news and blog posts is essential. One of the easiest ways to do this is by subscribing to RSS feeds, which allow you to receive the latest updates from your favorite websites directly in your RSS reader.
RSS (Really Simple Syndication) is a format that is used in many websites which allow web publisher to syndicate their latest posts or data automatically.
There is another method that allows the user to stay updated is bookmarking. But users need to manually go to websites on a timely basis and check what new has been added.
In this tutorial, I will show how you can use PHP to read RSS feeds of websites and show recent post lists using it.
Contents
1. HTML and PHP
HTML
Create a <form >
. In the <form >
create a textbox and a button. The textbox is use to enter the feed URL.
PHP
Assigned default feed URL in $url
variable, its value gets updated when <form >
is submitted.
Using simplexml_load_file()
function check URL is valid or not. If URL is valid then it returns an Object after interpreting the XML.
Loop over the Object to get content.
Completed Code
<div class="content"> <form method="post" action=""> <input type="text" name="feedurl" placeholder="Enter website feed URL"> <input type="submit" value="Submit" name="submit"> </form> <?php $url = "https://mintcream-cheetah-602163.hostingersite.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 "<h2>".$site."</h2>"; 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>
2. CSS
.content{ width: 60%; margin: 0 auto; } input[type=text]{ padding: 5px 10px; width: 60%; letter-spacing: 1px; } input[type=submit]{ padding: 5px 15px; letter-spacing: 1px; border: 0; background: gold; color: white; font-weight: bold; font-size: 17px; } h1{ border-bottom: 1px solid gray; } 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; }
3. Demo
4. Conclusion
Reading RSS feeds using PHP is a simple and powerful way to stay up to date with the latest news and blog posts from your favorite websites. By following above mentioned steps in this article, you can create a basic RSS reader that loads and parses an XML file, extracts the information you need, and outputs it in a useful format.
Use simplexml_load_file()
function to read RSS feeds of the website. Provide your feed URL into the function, then loop through the returned Object to view the contents.
If data is not loading then check the XML file structure and update the code accordingly.
If you found this tutorial helpful then don't forget to share.
Hello yogesh. i have run you script but i m getting internal server error for that script. please help me.
Hello
How can i parse rss links like this:
https://worldoftanks.eu/en/rss/news/
https://worldoftanks.eu/en/news/general-news/
with your parser…
Have php 7.3
Thanks in advance
ChileCaliente
using a strip_tags() PHP function : $description = strip_tags($item->description);
Just a little problem.
If content have html tags, like opening a DIV, the list is KO.
You have to add a strip_tags()
Like this : $description = strip_tags($item->description);
Hello Yogesh,
I’m fairly new to PHP and I’m trying to make sense of RSS Feeds. How would I go about starting the page without the initial Makeitweb Feed and just the form by itself? I tried moving things around and taking away the initial URL link within $url, but nothing seems to work.
Thank you.
Excellent script, how difficult would it be to also grab & display images when the feed includes an image with a particular article ?
What’s hot
Thu, 12 Dec 2019 08:37:00 -0800
<![CDATA[
Few areas of the enterpris
]]>
https://www.net/article/348/whag.html#tk.rss_all
Thanks for the article.its very help full
How can I get also the images from this?
Great code. worked perfectly for my project. Please assist on how i can order the results by date in descending order, ie starting with the latest article on top.
Hi Yogesh
I am quite new to php. Can you assist me on how i insert values into an array. i believe this should be a multidimensional array?
how to read only new entires on rss if i want to add cronjob and read only and write to database without deplicate contents
Thank you for this simple rss feed reader. It helped us same money and use your feed reader. Many many thanks :)!!!
Hello Yogesh thanks for your code, it is very helpfull.
i have a problem
the code work perfectly in localhost but when i put it online “invalid rss feed url”.
Any idea for this error please?
I have the same problem. Works beautifully on my local WordPress install but the same code won’t work on the live site. I’m thinking its related to the Sucuri firewall.
Hi bro, How to do the same for Dynamic PHP Mysql Website i tried many ways but not gets anything
My website is Custom made Dynamic php mysqli website.. You can Check my site as well bro cricketulagam.com
How can make Rss Feed for it
Pls Reply bro