PHP XML Reading RSS and Atom Feeds - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP XML Reading RSS and Atom Feeds - 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



15 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 is a truly good site post. Not too many people would actually, the way you just did. I am really impressed that there is so much information about this subject that have been uncovered and you’ve done your best, with so much class. If wanted to know more about green smoke reviews, than by all means come in and check our stuff. niche releated backlinks

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

    ReplyDelete
  8. Hello, Neat post. There is an issue along with your website in internet explorer, might test this¡K IE nonetheless is the marketplace chief and a big section of people will pass over your excellent writing due to this problem. web designer

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

    ReplyDelete
  10. 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
  11. The article posted was very informative and useful. You people are doing a great job. Keep going. device mockup

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

    ReplyDelete
  13. 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
  14. 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
  15. Needed to compose you a tiny note to finally thank you very much yet again for your personal splendid methods you have discussed above. It is strangely open-handed with people like you to provide publicly all that a number of people would have marketed as an electronic book to generate some bucks for their own end, primarily now that you could possibly have tried it if you ever wanted. These inspiring ideas likewise acted like a fantastic way to know that the rest have the same dreams really like my personal own to see a whole lot more concerning this problem. I’m sure there are thousands of more enjoyable times in the future for many who check out your blog. webflow developer

    ReplyDelete

Post Top Ad