适用程序:Typecho
程序版本:1.2.1
使用方法
打开文件 admin/index.php 找到第 115 行的如下代码:
html = cache ? cache.getItem('feed') : '',
将以上代码替换为以下代码:
html = '',
打开文件 var/Widget/Ajax.php 搜索找到 function feed() 将函数 {...} 内的所有代码,替换为以下代码(留意注释,以及按需修改):
$cacheFilePath = __TYPECHO_ROOT_DIR__ . '/usr/cache/Typecho_feed_cache.json'; // 将官方最新日志数据存储在 usr/cache 目录的 Typecho_feed_cache.json 文件中,自行修改
$cacheExpiration = 180*24*60*60; // 缓存半年,自行修改
if (file_exists($cacheFilePath) && (time() - filemtime($cacheFilePath)) < $cacheExpiration) {
$data = json_decode(file_get_contents($cacheFilePath), true);
$this->response->throwJson($data);
} else {
$this->user->pass('subscriber');
$client = Client::get();
if ($client) {
$client->setHeader('User-Agent', $this->options->generator)
->setTimeout(10)
->send('https://typecho.org/feed/');
// 匹配内容体
$response = $client->getResponseBody();
preg_match_all(
"/<item>\s*<title>([^>]*)<\/title>\s*<link>([^>]*)<\/link>\s*<guid>[^>]*<\/guid>\s*<pubDate>([^>]*)<\/pubDate>/is",
$response,
$matches
);
$data = [];
if ($matches) {
foreach ($matches[0] as $key => $val) {
$data[] = [
'title' => $matches[1][$key],
'link' => $matches[2][$key],
'date' => date('n.j', strtotime($matches[3][$key]))
];
if ($key > 8) {
break;
}
}
}
file_put_contents($cacheFilePath, json_encode($data));
$this->response->throwJson($data);
}
}
throw new Exception(_t('禁止访问'), 403);
注意:以上代码中,未对存储文件夹做是否可写的判断,所以,请确保文件夹可以创建缓存文件。