工作方式
先用PHP作为接口将Feed转换为Json,再由Js输出为HTML。
接口部分PHP
<?php
$feedParam = isset($_GET['feed'])? explode(',', $_GET['feed']) : [];
$rssSources = [];
if (!empty($feedParam)) {
$rssSources = array_map(function ($feed) {
return $feed. 'feed/';
}, $feedParam);
}
if (!empty($rssSources)) {
$allItems = array_reduce($rssSources, function ($carry, $source) {
$rss = simplexml_load_file($source);
foreach ($rss->channel->item as $item) {
$carry[] = [
'title' => (string)$item->title,
'description' => (string)$item->description,
'link' => (string)$item->link,
'pubDate' => (string)$item->pubDate,
'creator' => (string)$item->children('http://purl.org/dc/elements/1.1/')->creator,
];
}
return $carry;
}, []);
usort($allItems, fn($a, $b) => strtotime($b['pubDate']) - strtotime($a['pubDate']));
header('Content-Type: application/json');
echo json_encode(['items' => $allItems]);
} else {
header('Content-Type: application/json');
echo json_encode(['description' => 'No valid feed sources provided']);
}
?>
主题自定义页面部分
<div id="FeedLoader">正在加载中…<br>若长时间无响应,请尝试刷新页面</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const container = document.getElementById('FeedLoader');
fetch('https://i.eastblues.cn/feed.php?feed=https://i.eastblues.cn/feed/,URL2,URL3')
.then(response => response.json())
.then(data => {
container.innerHTML = '';
data.items.forEach(({ title, creator, pubDate, description, link }) => {
container.innerHTML += `
<h3>${title}</h3>
<p>作者:${creator}</p>
<p>发布日期:${pubDate}</p>
<p>${description}</p>
<p><a href="${link}" target="_blank">查看详情</a></p>
<hr>
`;
});
})
.catch(error => {
container.innerHTML = `<p>数据获取失败!</p>`;
});
});
</script>