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



No comments:

Post a Comment

Post Top Ad