0

API Mashup

This example is a mashup of diafol's class, Dani's parsing code and my class.

It uses diafol's dwAPI class to retrieve a list of all forums, loops through them, and uses my Rss class to get the respective RSS feeds. It will show each forum in a separate div (with Dani's code) with it's title and no more than the five latest posts. The div will be omitted if there are no posts. (@Dani: some feeds give a parse error, and that's shown instead.)

You can try it yourself. Note that looping through all the forums takes some time, that's why the set_time_limit(0) was included. If you want to see the result, look here.

Edited by pritaeas

<!doctype html>
<html>
<head>
    <title>Slow Loading! Five Latest Posts per Forum</title>
    <style>
        body {
            padding: 10px;
        }
        div {
            width: 300px;
            height: 200px;
            float: left;
            font-size: .8em;
            overflow: hidden;
            margin: 10px;
            padding: 5px;
            border: 1px dotted #752f8e;
        }
        h2, h3 {
            color: #752f8e;
        }
    </style>
</head>
<body>
<h2>Five Latest Posts per Forum</h2>
<?php
set_time_limit(0);

include 'dwapi.class.php';
include 'Rss.class.php';

function ShowFeed($forumTitle, $forumId)
{
    $rss = new Rss();

    $feed = $rss->GetFeed((int)$forumId, null);
    if ($feed)
    {
        try
        {
            $articles = new SimpleXMLElement($feed);
            $postCount = 0;
            if (count($articles->channel->item) > 0)
            {
                echo '<div><h3>' . $forumTitle . '</h3>';
                foreach ($articles->channel->item as $article)
                {
                    echo '<a href="' . $article->link . '">' . $article->title . '</a><br />';
                    $postCount++;
                    if ($postCount == 5)
                    {
                        break;
                    }
                }
                echo '</div>';
            }
        }
        catch (Exception $e)
        {
            echo '<div><h3>' . $forumTitle . '</h3>';
            echo $e->getMessage();
            echo '</div>';
        }
    }
}

$dw = new dwAPI();
$forums = $dw->json2Array($dw->getForums());
foreach ($forums['data'] as $mainForum)
{
    ShowFeed($mainForum['title'], $mainForum['id']);
    if (isset($mainForum['relatives']['children']) and count($mainForum['relatives']['children']) > 0)
    {
        foreach ($mainForum['relatives']['children'] as $subForum)
        {
            ShowFeed($subForum['title'], $subForum['id']);
            if (isset($subForum['relatives']['children']) and count($subForum['relatives']['children']) > 0)
            {
                foreach ($subForum['relatives']['children'] as $subSubForum)
                {
                    ShowFeed($subSubForum['title'], $subSubForum['id']);
                }
            }
        }
    }
}
?>
</body>
</html>
3
Contributors
3
Replies
73
Views
4 Years
Discussion Span
Last Post by diafol
1

I'll take a look as to why some of the RSS feeds are giving parse errors. However, it should be noted that you can accomplish the same thing, but significantly faster, by following the following algorithm:

Fetch list of all forums in JSON format
Create an empty array for each forum
Fetch page 1 of recent threads in JSON format
Hash the recent threads by forum (i.e. push onto its forum's array)
Set n = 2
While forums with fewer than 5 elements exist:
    Foreach forum that has fewer than 5 elements:
        Fetch page n of recent threads only in those forums in JSON format
        Hash the recent threads by forum
        n++
    End foreach
End while
1

+1 Agreed. I haven't considered performance. This is not how I would write production code anyway (it lacks caching and a responsive front-end). It was just to show how to use different pieces of code together. I'll let others optimize it.

0

I'll let others optimize it.

Yep. Chuck up the starter code, others can 'mod and opt' it to suit their own needs.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.