我在 [链接登录后可见],是直接用了插件,昨天重新折腾了独立API接口,需要的自取
这个API实现提供了以下主要功能:
文章相关接口:
GET /api.php?route=posts - 获取文章列表
GET /api.php?route=post&id={id} - 获取单篇文章详情
分类相关接口:
GET /api.php?route=categories - 获取所有分类
标签相关接口:
GET /api.php?route=tags - 获取所有标签
评论相关接口:
GET /api.php?route=comments - 获取评论列表
每个接口都支持以下特性:
分页支持(通过 page 和 pageSize 参数)
JSON 格式的响应
错误处理
跨域支持
使用方法:
将此文件保存为 api.php 并放在 Typecho 根目录
通过 HTTP 请求访问对应的接口
例如获取文章列表:
CopyGET [链接登录后可见]
<?php
/**
* Typecho REST API Implementation
* 此文件需放在 Typecho 根目录下的 api.php
*/
// 设置允许跨域访问
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=UTF-8');
// 引入 Typecho 配置文件
require_once 'config.inc.php';
// 引入 Typecho 公共方法
require_once 'var/Typecho/Common.php';
// 初始化数据库连接
Typecho_Db::set($db);
$options = Typecho_Widget::widget('Widget_Options');
// 路由处理
$route = isset($_GET['route']) ? $_GET['route'] : '';
$method = $_SERVER['REQUEST_METHOD'];
// API 响应类
class ApiResponse {
public static function success($data) {
echo json_encode([
'status' => 'success',
'data' => $data
]);
}
public static function error($message, $code = 400) {
http_response_code($code);
echo json_encode([
'status' => 'error',
'message' => $message
]);
}
}
// API 路由处理
switch ($route) {
case 'posts':
if ($method === 'GET') {
getPosts();
}
break;
case 'post':
if ($method === 'GET' && isset($_GET['id'])) {
getPost($_GET['id']);
}
break;
case 'categories':
if ($method === 'GET') {
getCategories();
}
break;
case 'tags':
if ($method === 'GET') {
getTags();
}
break;
case 'comments':
if ($method === 'GET') {
getComments();
}
break;
default:
ApiResponse::error('Invalid route', 404);
break;
}
// 获取文章列表
function getPosts() {
$pageSize = isset($_GET['pageSize']) ? (int)$_GET['pageSize'] : 10;
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$db = Typecho_Db::get();
$select = $db->select('cid', 'title', 'slug', 'created', 'modified', 'text', 'authorId')
->from('table.contents')
->where('type = ?', 'post')
->where('status = ?', 'publish')
->order('created', Typecho_Db::SORT_DESC)
->page($currentPage, $pageSize);
$posts = $db->fetchAll($select);
$result = [];
foreach ($posts as $post) {
$result[] = [
'id' => $post['cid'],
'title' => $post['title'],
'slug' => $post['slug'],
'created' => date('Y-m-d H:i:s', $post['created']),
'modified' => date('Y-m-d H:i:s', $post['modified']),
'content' => $post['text'],
'author' => getAuthor($post['authorId'])
];
}
ApiResponse::success($result);
}
// 获取单篇文章
function getPost($id) {
$db = Typecho_Db::get();
$post = $db->fetchRow($db->select()
->from('table.contents')
->where('cid = ?', $id)
->where('type = ?', 'post')
->where('status = ?', 'publish')
->limit(1));
if (!$post) {
ApiResponse::error('Post not found', 404);
return;
}
$result = [
'id' => $post['cid'],
'title' => $post['title'],
'slug' => $post['slug'],
'created' => date('Y-m-d H:i:s', $post['created']),
'modified' => date('Y-m-d H:i:s', $post['modified']),
'content' => $post['text'],
'author' => getAuthor($post['authorId']),
'categories' => getPostCategories($post['cid']),
'tags' => getPostTags($post['cid'])
];
ApiResponse::success($result);
}
// 获取分类列表
function getCategories() {
$db = Typecho_Db::get();
$categories = $db->fetchAll($db->select()
->from('table.metas')
->where('type = ?', 'category'));
$result = [];
foreach ($categories as $category) {
$result[] = [
'id' => $category['mid'],
'name' => $category['name'],
'slug' => $category['slug'],
'description' => $category['description']
];
}
ApiResponse::success($result);
}
// 获取标签列表
function getTags() {
$db = Typecho_Db::get();
$tags = $db->fetchAll($db->select()
->from('table.metas')
->where('type = ?', 'tag'));
$result = [];
foreach ($tags as $tag) {
$result[] = [
'id' => $tag['mid'],
'name' => $tag['name'],
'slug' => $tag['slug']
];
}
ApiResponse::success($result);
}
// 获取评论列表
function getComments() {
$pageSize = isset($_GET['pageSize']) ? (int)$_GET['pageSize'] : 10;
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$db = Typecho_Db::get();
$comments = $db->fetchAll($db->select()
->from('table.comments')
->where('status = ?', 'approved')
->order('created', Typecho_Db::SORT_DESC)
->page($currentPage, $pageSize));
$result = [];
foreach ($comments as $comment) {
$result[] = [
'id' => $comment['coid'],
'postId' => $comment['cid'],
'author' => $comment['author'],
'email' => $comment['mail'],
'url' => $comment['url'],
'content' => $comment['text'],
'created' => date('Y-m-d H:i:s', $comment['created'])
];
}
ApiResponse::success($result);
}
// 辅助函数:获取作者信息
function getAuthor($authorId) {
$db = Typecho_Db::get();
$author = $db->fetchRow($db->select()
->from('table.users')
->where('uid = ?', $authorId));
return [
'id' => $author['uid'],
'name' => $author['name'],
'screenName' => $author['screenName'],
'mail' => $author['mail']
];
}
// 辅助函数:获取文章分类
function getPostCategories($postId) {
$db = Typecho_Db::get();
$categories = $db->fetchAll($db->select('table.metas.*')
->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
->where('table.relationships.cid = ?', $postId)
->where('table.metas.type = ?', 'category'));
$result = [];
foreach ($categories as $category) {
$result[] = [
'id' => $category['mid'],
'name' => $category['name'],
'slug' => $category['slug']
];
}
return $result;
}
// 辅助函数:获取文章标签
function getPostTags($postId) {
$db = Typecho_Db::get();
$tags = $db->fetchAll($db->select('table.metas.*')
->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
->where('table.relationships.cid = ?', $postId)
->where('table.metas.type = ?', 'tag'));
$result = [];
foreach ($tags as $tag) {
$result[] = [
'id' => $tag['mid'],
'name' => $tag['name'],
'slug' => $tag['slug']
];
}
return $result;
}