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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.