Question

I'm working on creating an AJAX powered RSS Feed. It is a single pane similar to Google Reader with a list of feeds on the left and the feed content on the right. When you click on any of the feeds this will trigger an ajax command (jquery's $.ajax()) that will trigger the RSSFeed class's public getFeed($feed_url) function (which in turn utilizes SimplePie to get the data needed.

This uses:

  • jQuery 1.9.1 to handle the AJAX calls and updating the browser
  • Twitter Bootstrap 2.3.0 for layout / normalization / styling
  • SimplePie 1.3.1 to handle the RSS feeds themselves.

When the page loads, I am using the getFeed($feed_url); to correctly get the data (this works):

<div class="row">
    <div class="span7" id="feedBody">
        <?php
           $RSSFeed->getFeed("http://rss1.smashingmagazine.com/feed/");
        ?>
    </div>
</div>

This works as expected. Then a feed item may be clicked and that would trigger the following AJAX command (alerts are there for debugging purposes):

"use strict";

//Update the feed
$("a.feedName").click(function(e) {
    e.preventDefault();
    var feedURL;
    feedURL = $(this).attr("href");
    $.ajax('model/RSSFeed.php', {
        data: {url: feedURL},
        beforeSend: function() {
            $("#feedBody").html("<div class=\"alert alert-info span7\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button><strong>Loading Feed&hellip;</strong>");
        },
        cache: false,
        success: function(result) {
            $("#feedBody").html(result);
            alert(result);
        },
        error: function(result) {
            $("#feedBody").hide();
            alert("Error!");
        }
    });
});

Then model/RSSFeed.php sees this request and should use the following to trigger getting the new content:

if (isset($_GET['url'])) {
    $RSS = new RSSFeed();
    $RSS->getFeed($_GET['url']);
} else if(isset($_POST['url'])) {
    $RSS = new RSSFeed();
    $RSS->getFeed($_POST['url']);
}

This calls my RSSFeed class which is as follows:

class RSSFeed {
    //Get the feed at the $feed_url and echo it out to the browser for the ajax request to display
    public function getFeed($feed_url) {
        $feed = new SimplePie($feed_url);
        $feed->init();
        $feed->handle_content_type();
        foreach ($feed->get_items() as $item) {
            $output = "<article>"
            . "<h3><a href=\"" . $item->get_permalink() . "\" title=\"" . $item->get_title() . "\" class=\"articleTitle\">" . $item->get_title() . "</a></h3><p>";

            if ($category = $item->get_category()) {
                $output .= $category->get_label() . " ";
            }

            $output .= $item->get_date();


            $output .= "</p><p>";

            $output .= shorten($item->get_description(), 600) . "<br /><br />" . "<a href=\"" . $item->get_permalink() . "\" title=\"Read More\" class=\"btn btn-info\">Read More</a>";

            $output .= "</p>";

            echo $output;
        }//end foreach($feed->get_items() as $item)
    }//end getFeed($feed_url)

    //Begin setting up to allow Google Reader takeout files to be imported into the database.
    public function importRSSFeeds($xmlFile, $DB) {
        $xml = simplexml_load_file($xmlFile);
        foreach($xml as $feed) {
            foreach($feed->outline as $thisFeed) {
                if($thisFeed->outline['type'] == "rss") {
                        $DB->addFeedToDatabase($thisFeed['text'], $thisFeed['title'], "folder", "", "");
                    foreach($thisFeed->outline as $feeds) {
                        $DB->addFeedToDatabase($feeds['text'], $feeds['title'], $feeds['type'], $feeds['xmlUrl'], $feeds['htmlUrl']);
                    }
                    echo "<br /><br />";
                }
            }
        }
    } //end importRSSFeeds($xmlFile)
    //Get the feeds from the database and display them on the left for the user.
    public function getFeedList() {
        $lastType = "";
        $DB = new Database();
        $result = $DB->returnFeedList();
        foreach($result as $individualFeed) {
                if($individualFeed['type'] == "folder") {
                    if ($lastType == "rss") {
                        echo "</ul></div>";
                    }
                    echo "<li><a href=\"#\" data-toggle=\"collapse\" data-target=\"#" . str_replace(" ", "", $individualFeed['title']) ."\"><i class=\"icon-folder-close\"></i>" . $individualFeed['title'] . "</a></li>";
                    echo "<div class=\"collapse in\" id=\"" . str_replace(" ", "", $individualFeed['title']) . "\">";
                    echo "<ul class=\"nav nav-list\">";
                } else if($individualFeed['type'] == "rss") {
                    echo "<li><a href=\"" . $individualFeed['xmlUrl'] . "\" class=\"feedName\">" . $individualFeed['title'] . "</a></li>";
                }
                $lastType = $individualFeed['type'];
            }
        $DB->closeDatabaseConnection();
    }
}//end class RSSFeed

When checking the javascript alerts, it is triggering the success: case but not returning the echo'd data. What piece am I missing to have the SimplePie class return the correct, echo'd data via AJAX to my id="feedBody" div? What am I missing?

EDIT: Firebug says the following regarding the XHR:

//Response Headers
Status: 200 OK
Connection  Keep-Alive
Content-Length  0
Content-Type    text/html
Date    Thu, 11 Apr 2013 19:54:33 GMT
Keep-Alive  timeout=5, max=100
Server  Apache
X-Powered-By    PHP/5.4.4

//Request Headers
Accept  text/html, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cookie  __utma=111872281.446718044.1365710024.1365710024.1365710024.1; __utmb=111872281.2.10.1365710024; __utmc=111872281; __utmz=111872281.1365710024.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
DNT 1
Host    localhost:8888
Referer http://localhost:8888/Charcoal-Bootstrap_RSSReader/
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0
X-Requested-With    XMLHttpRequest

Params appear to be passing over correctly (just one example): url http://iphone.keyvisuals.com/feed/

Full index.php: http://pastebin.com/UJiFuBvG

Full RSSFeed.php: http://pastebin.com/CGU2nQHB

Full main.js (handles the AJAX): http://pastebin.com/1pPQuPUx

Thanks!

Was it helpful?

Solution

The answer came when I really pushed the alerts/exceptions to debug it and it seems as though it was not correctly loading the simplepie file, thus causing a fatal error and it to die. Sorry about that! Thank you Brad though

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top