Jump to content

Discover RSS feed from an URL?


AudiS2

Recommended Posts

Hi guys I searched for this.

 

 

How to write a function that would return the RSS feed url (or list of URLs) for the given site ? I guess this comes down to loading the page and parsing it for 'link' tags with rel='alternate' attribute?

 

For example feeds for phpfreaks.comare hidden in here.

<link href="http://feeds.feedburner.com/phpfreaks" rel="alternate" type="application/atom+xml" title="Latest Content" />
<link href="http://feeds.feedburner.com/phpfreaks/tutorials" rel="alternate" type="application/atom+xml" title="Latest Tutorials" />
<link href="http://feeds.feedburner.com/phpfreaks/blog" rel="alternate" type="application/atom+xml" title="Latest Blog Posts" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 

Can someone provide me with the code?

 

 

Link to comment
https://forums.phpfreaks.com/topic/131838-discover-rss-feed-from-an-url/
Share on other sites

  • 2 weeks later...

My ways are not the best, but they work and since I hate regex this is how I would do it.

 

<?php

function grabRSSFeeds($website) {
    $file = file($website);

    foreach ($file as $line) {
        $line = strtolower($line);
        if (stristr($line, "link") !== false) {
             if (stristr($line, "application/rss+xml") !== false) {
				$line = str_replace("\"", "", $line);
				$line = str_replace("'", "", $line);
                    $splitIt = split("href=", $line);
				$linked = split(" ", $splitIt[1]); // split at first space
				$links[] = $linked[0];
             }
        }elseif (stristr($line, "<body")) {
            break; //kill the foreach if we reached the body tag.
        }
    }
   
    return $links;
}

echo '<pre>';
print_r(grabRSSFeeds('http://www.9news.com')); // should return an array of the links
echo '</pre>'
?>

 

Untested but should work.

 

EDIT: Added a ' and space check that "should" cover about any type of setup you would find.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.