我正在创建一个基于 AJAX 的 RSS 提要。它是一个类似于 Google Reader 的单一窗格,左侧是提要列表,右侧是提要内容。当您单击任何提要时,这将触发一个 ajax 命令 (jquery's $.ajax()
),该命令将触发 RSSFeed 类的公共getFeed($feed_url)
函数(进而利用 SimplePie 来获取所需的数据。
这使用:
- jQuery 1.9.1 处理 AJAX 调用和更新浏览器
- Twitter Bootstrap 2.3.0 用于布局/规范化/样式
- SimplePie 1.3.1 自己处理 RSS 提要。
当页面加载时,我正在使用getFeed($feed_url);
来正确获取数据(这有效):
<div class="row">
<div class="span7" id="feedBody">
<?php
$RSSFeed->getFeed("http://rss1.smashingmagazine.com/feed/");
?>
</div>
</div>
这按预期工作。然后可以单击提要项,这将触发以下 AJAX 命令(警报用于调试目的):
"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\">×</button><strong>Loading Feed…</strong>");
},
cache: false,
success: function(result) {
$("#feedBody").html(result);
alert(result);
},
error: function(result) {
$("#feedBody").hide();
alert("Error!");
}
});
});
然后model/RSSFeed.php
看到这个请求,应该使用以下内容来触发获取新内容:
if (isset($_GET['url'])) {
$RSS = new RSSFeed();
$RSS->getFeed($_GET['url']);
} else if(isset($_POST['url'])) {
$RSS = new RSSFeed();
$RSS->getFeed($_POST['url']);
}
这调用了我的 RSSFeed 类,如下所示:
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
检查 javascript 警报时,它会触发success:
案例但不返回回显数据。为了让 SimplePie 类echo
通过 AJAX 将正确的数据返回到我的id="feedBody"
div,我缺少什么?我错过了什么?
编辑: Firebug 对 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
参数似乎正确传递(仅一个示例):
url http://iphone.keyvisuals.com/feed/
完整的 index.php: http: //pastebin.com/UJiFuBvG
完整的 RSSFeed.php:http ://pastebin.com/CGU2nQHB
完整的 main.js(处理 AJAX):http ://pastebin.com/1pPQuPUx
谢谢!