siteurl . '/api/greader.php/accounts/ClientLogin?' .
http_build_query([
'Email' => $this->user,
'Passwd' => $this->password
]);
$response = file_get_contents($loginUrl);
if ($response === false) {
throw new Exception('Login failed');
}
if (preg_match('/Auth=(.*)/', $response, $matches)) {
$this->authToken = $matches[1];
return true;
}
return false;
}
// 获取文章列表
public function fetchArticles() {
$articlesUrl = $this->siteurl . '/api/greader.php/reader/api/0/stream/contents/reading-list?&n=1000';
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => "Authorization: GoogleLogin auth=" . $this->authToken
]
]);
$response = file_get_contents($articlesUrl, false, $context);
if ($response === false) {
throw new Exception('Failed to fetch articles');
}
$data = json_decode($response, true);
return $data['items'] ?? [];
}
// 获取订阅列表
public function fetchSubscriptions() {
$subscriptionsUrl = $this->siteurl . '/api/greader.php/reader/api/0/subscription/list?output=json';
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => "Authorization: GoogleLogin auth=" . $this->authToken
]
]);
$response = file_get_contents($subscriptionsUrl, false, $context);
if ($response === false) {
throw new Exception('Failed to fetch subscriptions');
}
$data = json_decode($response, true);
return $data['subscriptions'] ?? [];
}
// 格式化文章数据
private function formatArticles($articles, $subscriptions) {
$subscriptionMap = [];
foreach ($subscriptions as $subscription) {
$subscriptionMap[$subscription['id']] = $subscription;
}
$formattedArticles = [];
foreach ($articles as $article) {
$strippedContent = strip_tags($article['summary']['content']);
$short_desc = (strlen($strippedContent) > 99)
? substr($strippedContent, 0, 99) . '...'
: $strippedContent;
$subscriptionId = $article['origin']['streamId'];
$subscription = $subscriptionMap[$subscriptionId] ?? null;
$formattedArticles[] = [
'site_name' => $article['origin']['title'],
'title' => $article['title'],
'link' => $article['alternate'][0]['href'],
'time' => date('c', $article['published']),
'description' => $short_desc,
'icon' => $subscription
? $this->siteurl . '/' . basename($subscription['iconUrl'])
: ''
];
}
return $formattedArticles;
}
// 获取并处理所有数据
public function getAllArticles() {
try {
if (!$this->login()) {
throw new Exception('Login failed');
}
$articles = $this->fetchArticles();
$subscriptions = $this->fetchSubscriptions();
return $this->formatArticles($articles, $subscriptions);
} catch (Exception $e) {
error_log($e->getMessage());
return [];
}
}
}
// 使用类获取数据并显示
try {
$api = new FreshRSSAPI();
$articles = $api->getAllArticles();
// 对文章按时间排序
usort($articles, function ($a, $b) {
return strtotime($b['time']) - strtotime($a['time']);
});
// 设置每页显示的文章数量
$itemsPerPage = 30;
// 输出HTML
echo '
';
foreach (array_slice($articles, 0, $itemsPerPage) as $article) {
// 格式化发布时间
$date = new DateTime($article['time']);
$date->setTimezone(new DateTimeZone('Asia/Shanghai'));
$formattedDate = $date->format('Y年m月d日 H:i:s');
echo '
';
echo '
' . htmlspecialchars($article['title']) . '
';
echo '
' . htmlspecialchars($article['description']) . '
';
echo '
';
echo '';
}
echo '
';
} catch (Exception $e) {
echo '获取文章失败:' . htmlspecialchars($e->getMessage()) . '
';
}
?>