DaniWeb API RSS class

Updated pritaeas 1 Tallied Votes 547 Views Share

RSS Class

The code snippet is a PHP class for retrieving RSS feeds from the DaniWeb website. There are two exposed methods, one for retrieving the list of predefined article types, and another to get a specific RSS feed. If you pass parameters to this method, there is a basic check to see if the forum ID is an integer and greater than zero. The article type will be checked against the predefined list.

To do

I want to combine this class later with the DaniWeb API, so it can retrieve a valid list of forum ID's to choose from. For now, I'll leave that as an exercise for you to add.

Another addition can be to add the XML parsing into the class, so it can return the result in an associative array.

Usage example

include 'Rss.class.php';

$rss = new Rss();

// Get the list of predefined article types.
$articleTypes = $rss->GetArticleTypes();

// Get the RSS feed for unanswered Hardware and Software quetions.
$feed = $rss->GetFeed(1, 'unanswered');

if ($feed)
{
    // Taken from the API documentation.
    $articles = new SimpleXMLElement($feed);
    foreach ($articles->channel->item as $article)
    {
        echo '<a href="' . $article->link . '">' . $article->title . '</a><br />';
    }
}
// Rss.class.php
class Rss {
    /**
     * @var array List of supported article types.
     */
    private $articleTypes = array (
        'unanswered', 'solved', 'news', 'reviews',
        'interviews', 'tutorials', 'code', 'whitepapers');

    /**
     * Returns the list of supported article types.
     *
     * @param bool $sorted Sort the list alphabetically, default true.
     * @return array List of article types.
     */
    public function GetArticleTypes($sorted = true)
    {
        $result = $this->articleTypes;
        if ($sorted)
        {
            sort($result);
        }
        return $result;
    }

    /**
     * Returns the RSS feed, optionally filtered by forum ID and/or article type.
     *
     * @param int $forumId Id of the forum, default null.
     * @param string $articleType Article type, default null.
     * @return mixed RSS feed, optionally filtered, false on error.
     */
    public function GetFeed($forumId, $articleType)
    {
        $urlParts = array ();
        $urlParts[] = 'http://www.daniweb.com/rss/pull';
        if (is_int($forumId) and ($forumId > 0))
        {
            $urlParts[] = $forumId;
        }
        if (in_array($articleType, $this->articleTypes))
        {
            $urlParts[] = $articleType;
        }
        return $this->GetUrl(implode('/', $urlParts));
    }

    /**
     * Get an URL and return the page contents as a string.
     *
     * @param string $url URL to get.
     * @return mixed URL page contents, false on error.
     */
    private function GetUrl($url)
    {
        $result = false;
        if (extension_loaded('curl'))
        {
            $ch = curl_init();
            if ($ch)
            {
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $curlResult = curl_exec($ch);
                if ($curlResult)
                {
                    $result = $curlResult;
                }
                curl_close($ch);
            }
        }
        else if (ini_get('allow_url_fopen'))
        {
            $result = file_get_contents($url);
        }
        return $result;
    }
}
Member Avatar for diafol
diafol

Nice cURL example. +1

Although file_get_contents can often be disabled by the host, would you suggest that method over cURL? Just asking out of curiosity as my experience with cURL is limited to a 'fallback' if file_* operations are not allowed.

OK, you've started the ball rolling, I feel under pressure to clean up and publish my own classes now
:(.

Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member

Personally, I live in a world where file_get_contents() is rather mainstream, but I also live in a world revolving around applications meant to be run on dedicated servers as opposed to shared hosting environments.

Member Avatar for diafol
diafol

Thanks for the input Dani. As ideas for the 'contest' start to warm up, a number of us - without the funds for dedicated servers - would like to get some example code up and ready for testing. Although my host allows 'allow_url_fopen', I know many hosts don't. Just wondering if cURL has any other advantages over the file_* methods. If so, it may make using file_* pointless in any classes that I produce. I admit, I find the file_* method a heck of a lot easier.

Apologies p - I didn't mean to hijack the thread with this. Do you have any thoughts on the cURL/file_* issue?

Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member

Well cURL certainly does have advantages over file_get_contents() in that it allows you to manually set headers, manage redirect handling, send POST requests, etc.

pritaeas commented: Agreed. +14
Member Avatar for diafol
diafol

OK, thanks :)

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

OK, you've started the ball rolling, I feel under pressure to clean up and publish my own classes now

LOL get to it then ;)

Hope to publish a class for the open API part next. After that the OAuth part.

Do you have any thoughts on the cURL/file_* issue?

What Dani said. In my experience the file_xxx is usually blocked, hence my favour to curl.

Member Avatar for diafol
diafol

Yep, I was thinking more of the straight retrieval when I asked the question, but now that you mention POST etc - fair one. Just published a getForums class, but probably needs a bit of tinkering.

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

Update to the code, to fall back on file_get_contents if possible.

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

More recent updates are available on GitHub. Rss class now inherits from a Base class containing shared functionality. Also started code for the read only API part. Once done I'll post another code snippet for that class.

Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member

Update to the code, to fall back on file_get_contents if possible.

Don't you mean to first try file_get_conents and then fallback on cURL?

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

Well no, in my case the file open wrappers are always disabled.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.