正在加载一言...
蘑菇一言API提供了简单的HTTP接口,用于获取和查询一言数据。所有公开接口都不需要认证即可使用。
获取一条随机的一言数据,可以按类型筛选。
GET /api/random.php
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
token |
string | 条件 | API访问令牌,非允许域名必填 |
c |
string | 否 | 一言类型,不填则随机返回(精确匹配) |
GET /api/random.php?c=动漫
{ "content": "我们的心就像那天空一样,永不分离。", "author": "天空之城", "type": "动漫" }
根据多种条件查询一言数据,支持按类型、作者筛选,以及分页和随机排序。
GET /api/index.php
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
token |
string | 条件 | API访问令牌,非允许域名必填 |
type |
string | 否 | 一言类型(支持模糊匹配) |
author |
string | 否 | 作者名称(支持模糊匹配) |
content |
string | 否 | 内容关键词(支持模糊匹配) |
random |
boolean | 否 | 是否随机排序,默认false |
single |
boolean | 否 | 是否只返回一条,默认false |
mode |
string | 否 | 查询模式,可选值:"search"(搜索)或"random"(随机) |
GET /api/index.php?type=动漫&random=true&single=true
GET /api/index.php?author=网络&mode=random&single=true
{ "status": "success", "page": 1, "per_page": 15, "total": 1, "data": [ { "Number": "156", "content": "我们的心就像那天空一样,永不分离。", "author": "天空之城", "type": "动漫" } ] }
<div id="yiyan">加载中...</div> <script> fetch('https://gordonsky.cn/yiyan/api/random.php') .then(response => response.json()) .then(data => { document.getElementById('yiyan').innerHTML = `${data.content} —— ${data.author}`; }) .catch(error => console.error('Error:', error)); </script>
<?php $response = file_get_contents('https://gordonsky.cn/yiyan/api/random.php'); $data = json_decode($response, true); if ($data) { echo $data['content'] . ' —— ' . $data['author']; } else { echo '获取一言失败'; } ?>
<!-- 在页面中添加标题元素 --> <h1>加载中...</h1> <h3></h3> <script type="text/javascript"> var xhr = new XMLHttpRequest(); xhr.open('get', 'https://gordonsky.cn/yiyan/api/random.php'); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { var data = JSON.parse(xhr.responseText); // 处理不同的响应格式 if (data.status === 'success' && data.data) { // 新的API格式 document.getElementsByTagName('h1')[0].innerText = data.data.content; document.getElementsByTagName('h3')[0].innerText = data.data.author; } else if (data.content) { // 旧的API格式 document.getElementsByTagName('h1')[0].innerText = data.content; document.getElementsByTagName('h3')[0].innerText = data.author; } } } xhr.send(); </script>
<!-- 在页面中添加标题元素 --> <h1>加载中...</h1> <h3></h3> <script type="text/javascript"> // 指定要查找的作者 var targetAuthor = '网络'; // 使用循环查询,直到找到作者包含“网络”的数据 function fetchNetworkQuote(maxAttempts = 10) { let attempts = 0; function tryFetch() { attempts++; console.log(`尝试第 ${attempts} 次,共 ${maxAttempts} 次`); return fetch('https://gordonsky.cn/yiyan/api/random.php') .then(response => response.json()) .then(data => { // 检查不同的响应格式 let content, authorName; if (data.status === 'success' && data.data) { content = data.data.content; authorName = data.data.author; } else if (data.content) { content = data.content; authorName = data.author; } else { throw new Error('无效的响应格式'); } // 检查作者是否包含目标字符串 if (authorName && authorName.includes(targetAuthor)) { return { content, author: authorName }; } else if (attempts < maxAttempts) { // 继续尝试 return tryFetch(); } else { // 超过最大尝试次数,返回当前结果 return { content, author: authorName }; } }); } return tryFetch(); } fetchNetworkQuote() .then(result => { document.getElementsByTagName('h1')[0].innerText = result.content; document.getElementsByTagName('h3')[0].innerText = result.author; }) .catch(error => { console.error('Error:', error); document.getElementsByTagName('h1')[0].innerText = '获取数据失败'; document.getElementsByTagName('h3')[0].innerText = 'Error'; }); </script>