PHP XML Reading RSS and Atom Feeds - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP XML Reading RSS and Atom Feeds - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, June 16, 2019

PHP XML Reading RSS and Atom Feeds

PHP XML 


Reading RSS and Atom Feeds


Problem

You want to retrieve RSS and Atom feeds and look at the items. This allows you to incorporate newsfeeds from multiple websites into your application.

Solution

Use the MagpieRSS parser. Here’s an example that reads the RSS feed for the php.announce mailing list:

           require __DIR__ . '/magpie/rss_fetch.inc';

           $feed = 'http://news.php.net/group.php?group=php.announce&format=rss';

           $rss = fetch_rss( $feed );

           print "<ul>\n";
           foreach ($rss->items as $item) {
                 print '<li><a href="' . $item['link'] . '">' . $item['title'] .
                         "</a></li>\n";
           }
           print "</ul>\n";

Discussion

RSS is an easy-to-use headline or article syndication format written in XML. Many news websites, such as the New York Times and the Washington Post, provide RSS feeds that update whenever new stories are published. 

Weblogs have also embraced RSS and having an RSS feed for your blog is a standard feature. The PHP website also publishes RSS feeds for most PHP mailing lists. Atom is a similar XML syndication format. It extends many of the concepts in RSS, including a way to read and write Atom data. 

It also attempts to provide a more well-defined syntax for syndication than RSS, because the RSS specification doesn’t always clearly enumerate exactly what is or isn’t permissible in a feed.

Using MagpieRSS, retrieving and parsing RSS and Atom feeds are simple:

           $feed = 'http://news.php.net/group.php?group=php.announce&format=rss';

           $rss = fetch_rss($feed);

This example reads in the RSS feed for the php.announce mailing list. The feed is then parsed by fetch_rss() and stored internally within $rss.

Although this feed is RSS 0.93, there’s no need to specify this to MagpieRSS. Its fetch_rss() function detects the syndication format, including Atom, and formats the document accordingly.

Each RSS item is then retrieved as an associative array using the items property:

           print "<ul>\n";

           foreach ($rss->items as $item) {
                  print '<li><a href="' . $item['link'] . '">' . $item['title'] .
                          "</a></li>\n";
           }
           print "</ul>\n";

This foreach loop creates an unordered list of items with the item title linking back to the URL associated with the complete article. Besides the required title and link fields, an item can have an optional description field that contains a brief write-up about the item.



Figure  php.announce RSS feed


Each channel also has an entry with information about the feed, as shown in Figure To retrieve this data, access the channel attribute:

           $feed = 'http://news.php.net/group.php?group=php.announce&format=rss';
           $rss = fetch_rss($feed);

                   print "<ul>\n";

                   foreach ($rss->channel as $key => $value) {
                           print "<li>$key: $value</li>\n";
                   }

                   print "</ul>\n";



Figure  php.announce RSS channel information



33 comments:

  1. Thanks so much for another post. I be able to get that kind of information information. friend, and exactly. website design company

    ReplyDelete
  2. Good post , I am going to spend more time researching this topic website design

    ReplyDelete
  3. Hello to all I cannot understand the way to add your website in my rss reader. Assist me, please ui/ux design

    ReplyDelete
  4. The good intreguing articles keep me coming back here time and time again. thank you so much. development studios

    ReplyDelete
  5. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! antminer s17 tutorial

    ReplyDelete
  6. This bit of blog shows responsibility of writer.
    Canvas Prints

    ReplyDelete
  7. Good post , I am going to spend more time researching this topic ui/ux design

    ReplyDelete
  8. What I wouldnt get for possess a controversy along with you relating to this. You just let them know a lot of things that come from nowhere fast in which Internet marketing fairly certain Identity use a fair picture. Your weblog is extremely good creatively, After all people will not be bored stiff. But other people who is able to see past the video clips and also the design will not be thus pleased using your generic knowledge of this specific matter. iphone mockup

    ReplyDelete
  9. The article posted was very informative and useful. You people are doing a great job. Keep going. device mockup

    ReplyDelete
  10. Utterly composed subject material , thanks for selective information . android mockups

    ReplyDelete
  11. Nice blog and absolutely outstanding. You can do something much better but i still say this perfect.Keep trying for the best. Anonymous Live

    ReplyDelete
  12. Interesting website, i read it but i still have a few questions. shoot me an email and we will talk more becasue i may have an interesting idea for you. webflow agency

    ReplyDelete
  13. As I website owner I conceive the content material here is rattling excellent , thanks for your efforts. webflow agency

    ReplyDelete
  14. Keep in touch whilst functioning from your own home office with out all of the hassle of purchasing or procurment costly office equipment. Debtors are allowed to apply with their a bad credit score background whenever. webflow company

    ReplyDelete
  15. Glad to be one of the visitors on this awe inspiring web site : D. webflow designers

    ReplyDelete
  16. Nice site. On your blogs very interest and i will tell a friends. web development companies

    ReplyDelete
  17. This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll definitely discover it. best web development company

    ReplyDelete
  18. This is an amazing blog, thank you so much for sharing such valuable information with us.
    Professionele webshop laten maken

    ReplyDelete
  19. You are very cool! I dont suppose I have read something similar to this before. So nice to search out somebody with authentic applying for grants this subject. I really appreciate starting this up. Your website is one area that is needed on the net. Totally a helpful project for bringing new things for the web! app screenshot

    ReplyDelete
  20. We structure Responsive, Web based business, Static and Dyanamic.Webdesign

    ReplyDelete
  21. Yes i am totally agreed with article and i want to say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!THANKS!!!!!! software development company

    ReplyDelete
  22. "AlizaSolutions designers have created and implemented logos for start-ups and well-established businesses alike. The one constant is our commitment to studying and understanding an organization, then creating a logo that captures its identity.
    Website:https://www.alizasolutions.com/
    Address:1st Floor Alperton House, London, UK, HA0 1EH.
    Phone:0044 7449 186 664" Business logo designing services

    ReplyDelete
  23. I don’t make it a habit to make comments on many articles, but this one deserves attention. I agree with the data you have written so eloquently here. Thank you. apple watch vector

    ReplyDelete
  24. Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work. here

    ReplyDelete
  25. Interesting website, i read it but i still have a few questions. shoot me an email and we will talk more becasue i may have an interesting idea for you. user experience design

    ReplyDelete
  26. My partner and i still can not quite assume that I could be one of those reading through the important recommendations found on this blog. My family and I are sincerely thankful for your generosity and for presenting me potential to pursue the chosen career path. Appreciate your sharing the important information I acquired from your web-site. user interface design

    ReplyDelete
  27. Admiring the time and effort you put into your blog and detailed information you offer!.. website development company

    ReplyDelete
  28. I read this blog very carefully, the articles were very nice I got lots of benefits from articles, I have a website related to this, if you want you can visit this website, believe will benefit, thanks.
    ecommerce web development melbourne

    ReplyDelete
  29. I have been reading out some of your articles and i must say nice stuff. I will definitely bookmark your blog. front end development company

    ReplyDelete
  30. Often the Are generally Weight reduction plan is unquestionably an low-priced and flexible weight-reduction plan product modeled on individuals seeking out shed some pounds combined with at some point maintain a far healthier your life. la weight loss web design agency new york

    ReplyDelete
  31. The very first step to creating a new site and signing up with aninternet host is selecting a special domain name. Everything you want to
    construct a web site, they sell.visit here

    ReplyDelete
  32. This is a appropriate blog for everyone who is wishes to find out about this topic. You understand a great deal its practically challenging to argue with you (not that I just would want…HaHa). You certainly put a brand new spin on a topic thats been discussed for years. Great stuff, just fantastic! ny web design firms

    ReplyDelete
  33. There is noticeably a bundle to learn about this. I assume you made sure good points in features also. best branding agencies san francisco

    ReplyDelete

Post Top Ad