如果您已经使用 SimplePie,那么您可以使用它的caching mechanism 来缓存提要数据。
要合并来自内部和外部来源的文章,请创建一个包含所有文章的数据结构。这可以是按发布时间戳排序的所有项目的数组。然后从这个数组中选择特定页码的文章。
下面是一些用于创建帖子组合数组的代码。这应该让您了解所涉及的步骤。 Post 类代表一个帖子。内部和外部帖子被转换为一个帖子并存储在数组 $posts 中。该数组按时间戳排序,最后所有帖子都会回显。
$internalPosts 必须包含来自您系统的帖子和 $feedUrls 外部供稿的 URL。由于我不知道内部帖子的结构,您必须调整内部帖子转换为通用帖子的部分。
$internalPosts = array();
$feedUrls = array();
include_once 'simplepie.inc';
class Post {
public $title;
public $link;
public $description;
public $publishedAt;
public function __construct($title, $link, $description, $publishedAt) {
$this->title = $title;
$this->link = $link;
$this->description = $description;
$this->publishedAt = $publishedAt;
}
}
$posts = array();
// Convert internal posts to generic post.
foreach($internalPosts as $item){
$posts[] = new Post($item->title, $item->link, $item->description, $item->publishedAt);
}
// Retrieve feeds and add posts.
$feed = new SimplePie();
foreach($feedUrls as $url){
$feed->set_feed_url($url);
$feed->init();
foreach ($feed->get_items() as $item) {
$posts[] = new Post($item->get_title(), $item->get_link(), $item->get_description(), $item->get_date('U'));
}
}
// Sort function.
function byPublicationTimestamp($itemA, $itemB){
return ($itemB->publishedAt - $itemA->publishedAt);
}
usort($posts, 'byPublicationTimestamp');
foreach($posts as $post){
echo "<p><a href='$post->link'>$post->title</a><br/>" . date('l, j F Y', $post->publishedAt) . " - $post->description</p>";
}
为了提高性能,请考虑单独存储合并的文章并根据这些数据构建页面。然后,您需要在内部发布新文章或刷新外部提要的缓存版本时更新此组合数据。
如果您需要在原始网站上发布后不久发布外部内容,那么我会联系这些网站,看看是否可以收到更新通知,而不是等待缓存版本过期。
编辑:添加示例代码。