PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Locating Feed URIs from Websites

These days, many websites are aware that the location of their XML feeds is not always obvious. A small RDF, RSS or Atom graphic helps when the user is reading the page, but what about when a machine visits trying to identify where your feeds are located? To assist in this, websites may point to their feeds using <link> tags in the <head> section of their HTML. To take advantage of this, you can use Zend_Feed_Reader to locate these feeds using the static findFeedLinks() method.

This method calls any URI and searches for the location of RSS, RDF and Atom feeds assuming the website's HTML contains the relevant links. It then returns a value object where you can check for the existence of a RSS, RDF or Atom feed URI.

The returned object is an ArrayObject subclass called Zend_Feed_Reader_Collection_FeedLink so you can cast it to an array, or iterate over it, to access all the detected links. However, as a simple shortcut, you can just grab the first RSS, RDF or Atom link using its public properties as in the example below. Otherwise, each element of the ArrayObject is a simple array with the keys "type" and "uri" where the type is one of "rdf", "rss" or "atom".

<?php
$links 
Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');

if(isset(
$links->rdf)) {
    echo 
$links->rdf"\n"// http://www.planet-php.org/rdf/
}
if(isset(
$links->rss)) {
    echo 
$links->rss"\n"// http://www.planet-php.org/rss/
}
if(isset(
$links->atom)) {
    echo 
$links->atom"\n"// http://www.planet-php.org/atom/
}

Based on these links, you can then import from whichever source you wish in the usual manner.

This quick method only gives you one link for each feed type, but websites may indicate many links of any type. Perhaps it's a news site with a RSS feed for each news category. You can iterate over all links using the ArrayObject's iterator.

<?php
$links 
Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');

foreach (
$links as $link) {
    echo 
$link['uri'], "\n";
}

Zend Framework