主要是在自己博客上面新建一个页面,输出友链的最新文章
一、在博客根目录下面创建一个 php 文件,例如 outputrss.php,代码如下
<?php
// 需要抓取的RSS链接数组及对应的图片地址
$feeds = [
['url' => '友链1的订阅地址', 'image' => '友链1的logo'],
['url' => '友链2的订阅地址', 'image' => '友链2的logo']
];
// 存储结果的数组
$results = [];
foreach ($feeds as $feed) {
// 使用file_get_contents获取RSS/Atom内容
$xml = file_get_contents($feed['url']);
// 使用SimpleXML解析RSS/Atom内容
$rss = simplexml_load_string($xml);
// 检查是否成功加载RSS/Atom
if ($rss === false) {
echo "Error loading feed from " . $feed['url'];
continue;
}
// 获取RSS/Atom源的标题
$siteTitle = (string)($rss->channel->title ?? $rss->title);
// 获取第一条item
$item = $rss->channel->item[0] ?? $rss->entry[0];
if ($item === null) {
echo "No items found in feed from " . $feed['url'];
continue;
}
// 格式化发布时间
$pubDate = date('Y-m-d H:i', strtotime((string)($item->pubDate ?? $item->published)));
// 将所需的信息存入数组
$results[] = [
'siteTitle' => $siteTitle,
'title' => (string)$item->title,
'link' => (string)($item->link['href'] ?? $item->link),
'description' => (string)($item->description ?? $item->summary),
'pubDate' => $pubDate,
'image' => $feed['image']
];
}
// 设置响应头为JSON
header('Content-Type: application/json');
// 输出JSON数据
echo json_encode($results, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// 保存JSON数据到文件
file_put_contents('outputrss.json', json_encode($results, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
?>
二、编辑上述代码
修改友链的 rss 订阅地址,有几个就加几个,旁边是对应的 logo,当然你也可以不添加 logo,这是我自己用的。最后修改倒数第二行的 outputrss.json,这个是生成的 json 文件名,也可以不改。
三、访问这个 php 文件
你的博客域名 /outputrss.php
四、新建页面
在你的博客模板下面新建页面模板,然后根据自己模板,来创建这个页面,以下是我的样式
<style type="text/css">
.friends {
display: flex;
align-items: stretch;
justify-content: space-between;
flex-wrap: wrap;
margin: -6px;
}
.friends .friend {
margin: 6px;
min-width: 240px;
flex: 1;
display: flex;
align-items: center;
justify-content: center;
border-radius: 10px;
border: 1px solid rgba(200, 200, 200, 0.2);
background-color: rgba(200, 200, 200, 0.1);
position: relative;
}
.friends .friend.friend-empty {
border: none;
background: none;
}
.friends .friend .friend-avatar {
flex-shrink: 0;
margin: 8px;
width: 60px;
height: 60px;
line-height: 60px;
border-radius: 30px;
background-color: rgba(200, 200, 200, 0.2);
background-size: cover;
background-position: center;
font-size: 24px;
text-align: center;
}
.friends .friend .friend-detail {
flex: 1;
width: 0;
display: flex;
flex-direction: column;
margin: 8px 8px 8px 0;
}
</style>
<div class="friends">
<?php
// 获取JSON数据
$jsonData = file_get_contents('./outputrss.json');
// 将JSON数据解析为PHP数组
$articles = json_decode($jsonData, true);
// 对文章按时间排序(最新的排在前面)
usort($articles, function ($a, $b) {
return strtotime($b['pubDate']) - strtotime($a['pubDate']);
});
// 设置每页显示的文章数量
$itemsPerPage = 12;
// 生成文章列表
foreach (array_slice($articles, 0, $itemsPerPage) as $article) {
$articles_list ='
<div class="friend">
<div class="friend-avatar" style="background-image: url(' . htmlspecialchars($article['image']) . ')"></div>
<div class="friend-detail">
<p>标题:' . htmlspecialchars($article['title']) . '</p>
<p>来源:' . htmlspecialchars($article['siteTitle']) . '</p>
<p>链接:<a href="' . htmlspecialchars($article['link']) . '" target="_blank">查看原文</a></p>
<p>时间:' . htmlspecialchars($article['pubDate']) . '</p>
</div>
</div>
';
echo $articles_list;
}
?>
<div class="friend friend-empty"></div>
<div class="friend friend-empty"></div>
<div class="friend friend-empty"></div>
</div>
五、把上述代码的获取 json 那里,改成你的 json 文件名即可
最后,展示地址如下: 揽星友圈
![图片[1]|在你的博客上面添加一个rss输出页面|不死鸟资源网](https://www.busi.net/wp-content/uploads/2025/06/20250613075700628-image-1024x718.png)
声明:代码是通义千问写的,样式是我去年抄来的,实在忘记在哪抄的了,抱歉作者。如果你不需要 logo,那就自己问 AI 改一改就行,该代码同时可以解析 rss 和 atom 格式,样式代码仅供参考,根据自己的博客整体样式来就行,可以让 AI 写样式。
该代码是只输出友链的最新一条,并且只展示最新的 12 个,可以在代码可以改显示数量,也可以按照自己需求,让 AI 输出更多的
本站资源均为作者提供和网友推荐收集整理而来,仅供学习和研究使用,请在下载后24小时内删除,谢谢合作!
THE END