Repository navigation
Expand file tree
/
Copy pathdocs.html
More file actions
488 lines (439 loc) · 13.8 KB
/
Copy pathdocs.html
File metadata and controls
488 lines (439 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>一言 API 文档</title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<h1>一言 API 文档</h1>
<p>一言 API 提供了随机名言、文本或图片的获取功能,以及内容提交接口。</p>
<h2>认证方式</h2>
<p>除了获取之外不需要认证,其余请求需要在请求头中包含以下认证信息:</p>
<ul>
<li><code>X-Client-ID</code>: 客户端ID</li>
<li><code>X-App-ID</code>: 应用ID</li>
<li><code>Authorization</code>: Bearer token (格式: <code>Bearer your_token</code>)</li>
</ul>
<h2>API 端点</h2>
<div>
<h3>获取随机内容</h3>
<p><span>GET</span> <code>/</code></p>
<p>返回一条随机的内容(文本或图片)。</p>
<h4>请求参数</h4>
<table>
<tr>
<th>参数</th>
<th>类型</th>
<th>说明</th>
</tr>
<tr>
<td>docs</td>
<td>boolean</td>
<td>设置为true可返回HTML格式的文档页面</td>
</tr>
</table>
<h4>代码示例</h4>
<h5>JavaScript (Fetch)</h5>
<pre><code class="language-javascript">fetch('https://api-yiyan.dfggmc.top/')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));</code></pre>
<h5>PHP</h5>
<pre><code class="language-php">$url = 'https://api-yiyan.dfggmc.top/';
$response = file_get_contents($url);
$data = json_decode($response, true);
print_r($data);</code></pre>
<h5>Python</h5>
<pre><code class="language-python">import requests
response = requests.get('https://api-yiyan.dfggmc.top/')
data = response.json()
print(data)</code></pre>
<h4>响应示例</h4>
<pre><code class="language-json">{
"meta": {
"version": "1.0.0",
"timestamp": 1620000000,
"status": "success",
"code": 200
},
"message": "成功检索随机引用",
"data": {
"id": 123,
"content": "这是一条随机名言",
"description": "来源描述",
"content_type": "text",
"add_time": 1620000000,
"user_name": "用户名"
}
}</code></pre>
</div>
<div>
<h3>获取所有内容</h3>
<p><span>GET</span> <code>/?all=true</code></p>
<p>返回所有非隐藏内容,支持分页。</p>
<h4>请求参数</h4>
<table>
<tr>
<th>参数</th>
<th>类型</th>
<th>说明</th>
</tr>
<tr>
<td>all</td>
<td>boolean</td>
<td>必须设置为true</td>
</tr>
<tr>
<td>page</td>
<td>integer</td>
<td>页码,默认为1</td>
</tr>
<tr>
<td>docs</td>
<td>boolean</td>
<td>设置为true可返回HTML格式的文档页面</td>
</tr>
</table>
<h4>代码示例</h4>
<h5>JavaScript (Fetch)</h5>
<pre><code class="language-javascript">fetch('https://api-yiyan.dfggmc.top/?all=true&page=1')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));</code></pre>
<h5>PHP</h5>
<pre><code class="language-php">$url = 'https://api-yiyan.dfggmc.top/?all=true&page=1';
$response = file_get_contents($url);
$data = json_decode($response, true);
print_r($data);</code></pre>
<h5>Python</h5>
<pre><code class="language-python">import requests
params = {'all': 'true', 'page': 1}
response = requests.get('https://api-yiyan.dfggmc.top/', params=params)
data = response.json()
print(data)</code></pre>
<h4>响应示例</h4>
<pre><code class="language-json">{
"meta": {
"version": "1.0.0",
"timestamp": 1620000000,
"status": "success",
"code": 200
},
"message": "成功检索",
"data": {
"quotes": [
{
"id": 123,
"content": "这是一条名言",
"description": "来源描述",
"content_type": "text",
"add_time": 1620000000,
"user_name": "用户名"
},
{
"id": 124,
"content": "https://example.com/data/image/image123.jpg",
"description": "图片描述",
"content_type": "image",
"add_time": 1620000001,
"user_name": "用户名"
}
],
"pagination": {
"total": 50,
"per_page": 20,
"current_page": 1,
"total_pages": 3,
"has_more": true
},
"last_update": 1620000000
}
}</code></pre>
</div>
<div>
<h3>提交新内容</h3>
<p><span>POST</span> <code>/</code></p>
<p>提交新的内容(文本或图片)。</p>
<h4>请求格式</h4>
<p>支持两种提交方式:</p>
<ol>
<li><strong>JSON格式</strong>:用于提交文本内容</li>
<li><strong>表单格式</strong>:用于提交图片内容</li>
</ol>
<h4>JSON 请求参数</h4>
<table>
<tr>
<th>参数</th>
<th>类型</th>
<th>必填</th>
<th>说明</th>
</tr>
<tr>
<td>content</td>
<td>string</td>
<td>是</td>
<td>内容文本</td>
</tr>
<tr>
<td>user_name</td>
<td>string</td>
<td>是</td>
<td>用户名</td>
</tr>
<tr>
<td>content_type</td>
<td>string</td>
<td>否</td>
<td>内容类型,默认为"text"</td>
</tr>
<tr>
<td>quote_source</td>
<td>string</td>
<td>否</td>
<td>来源描述</td>
</tr>
</table>
<h4>表单请求参数</h4>
<table>
<tr>
<th>参数</th>
<th>类型</th>
<th>必填</th>
<th>说明</th>
</tr>
<tr>
<td>content</td>
<td>string</td>
<td>是</td>
<td>内容描述</td>
</tr>
<tr>
<td>user_name</td>
<td>string</td>
<td>是</td>
<td>用户名</td>
</tr>
<tr>
<td>content_type</td>
<td>string</td>
<td>是</td>
<td>必须设置为"image"</td>
</tr>
<tr>
<td>image_file</td>
<td>file</td>
<td>是</td>
<td>图片文件</td>
</tr>
<tr>
<td>quote_source</td>
<td>string</td>
<td>否</td>
<td>来源描述</td>
</tr>
</table>
<h4>代码示例</h4>
<h5>JavaScript (Fetch) - JSON提交</h5>
<pre><code class="language-javascript">const data = {
content: "这是一条新名言",
user_name: "testuser",
quote_source: "名人名言"
};
fetch('https://api-yiyan.dfggmc.top/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Client-ID': 'your_client_id',
'X-App-ID': 'your_app_id',
'Authorization': 'Bearer your_token'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));</code></pre>
<h5>PHP - JSON提交</h5>
<pre><code class="language-php">$url = 'https://api-yiyan.dfggmc.top/';
$data = [
'content' => '这是一条新名言',
'user_name' => 'testuser',
'quote_source' => '名人名言'
];
$options = [
'http' => [
'header' => "Content-type: application/json\r\n" .
"X-Client-ID: your_client_id\r\n" .
"X-App-ID: your_app_id\r\n" .
"Authorization: Bearer your_token\r\n",
'method' => 'POST',
'content' => json_encode($data),
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
print_r(json_decode($result, true));</code></pre>
<h5>Python - JSON提交</h5>
<pre><code class="language-python">import requests
import json
url = 'https://api-yiyan.dfggmc.top/'
headers = {
'Content-Type': 'application/json',
'X-Client-ID': 'your_client_id',
'X-App-ID': 'your_app_id',
'Authorization': 'Bearer your_token'
}
data = {
'content': '这是一条新名言',
'user_name': 'testuser',
'quote_source': '名人名言'
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.json())</code></pre>
<h5>JavaScript (Fetch) - 图片上传</h5>
<pre><code class="language-javascript">const formData = new FormData();
formData.append('content', '这是一张图片描述');
formData.append('user_name', 'testuser');
formData.append('content_type', 'image');
formData.append('image_file', fileInput.files[0]);
fetch('https://api-yiyan.dfggmc.top/', {
method: 'POST',
headers: {
'X-Client-ID': 'your_client_id',
'X-App-ID': 'your_app_id',
'Authorization': 'Bearer your_token'
},
body: formData
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));</code></pre>
<h5>PHP - 图片上传</h5>
<pre><code class="language-php">$url = 'https://api-yiyan.dfggmc.top/';
$data = [
'content' => '这是一张图片描述',
'user_name' => 'testuser',
'content_type' => 'image',
'image_file' => new CURLFile('/path/to/image.jpg')
];
$headers = [
'X-Client-ID: your_client_id',
'X-App-ID: your_app_id',
'Authorization: Bearer your_token'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
print_r(json_decode($response, true));</code></pre>
<h5>Python - 图片上传</h5>
<pre><code class="language-python">import requests
url = 'https://api-yiyan.dfggmc.top/'
headers = {
'X-Client-ID': 'your_client_id',
'X-App-ID': 'your_app_id',
'Authorization': 'Bearer your_token'
}
files = {
'image_file': open('image.jpg', 'rb')
}
data = {
'content': '这是一张图片描述',
'user_name': 'testuser',
'content_type': 'image'
}
response = requests.post(url, headers=headers, files=files, data=data)
print(response.json())</code></pre>
<h4>响应示例</h4>
<pre><code class="language-json">{
"meta": {
"version": "1.0.0",
"timestamp": 1620000000,
"status": "success",
"code": 201
},
"message": "数据提交成功",
"data": {
"id": 125,
"content": "image123.jpg",
"content_type": "image",
"user_name": "用户名",
"quote_source": "来源描述",
"is_hidden": 1,
"add_time": 1620000000,
"content_url": "https://example.com/data/image/image123.jpg"
}
}</code></pre>
</div>
<h2>内容类型</h2>
<table>
<tr>
<th>类型</th>
<th>说明</th>
</tr>
<tr>
<td>text</td>
<td>文本内容</td>
</tr>
<tr>
<td>image</td>
<td>图片内容</td>
</tr>
</table>
<h2>图片上传限制</h2>
<ul>
<li>最大文件大小:5MB</li>
<li>支持的图片格式:JPEG, PNG, GIF, WebP</li>
</ul>
<div>
<strong>注意:</strong>通过API提交的内容默认会被标记为隐藏,需要管理员审核后才能公开显示。
</div>
<h2>错误代码</h2>
<table>
<tr>
<th>代码</th>
<th>说明</th>
</tr>
<tr>
<td>400</td>
<td>请求参数错误</td>
</tr>
<tr>
<td>401</td>
<td>未授权或认证失败</td>
</tr>
<tr>
<td>403</td>
<td>禁止访问</td>
</tr>
<tr>
<td>404</td>
<td>未找到内容</td>
</tr>
<tr>
<td>500</td>
<td>服务器内部错误</td>
</tr>
</table>
<footer>
© 花枫工作室 2025 | [Apache License]开源地址:<a href="https://github.com/flmpst/api-yiyan">https://github.com/flmpst/api-yiyan/</a>
</footer>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/base16/apprentice.min.css"
integrity="sha512-Xd2UwlrMhVcb1YJSAGyoiGUFgdRz526CH9BfL44s+6YFsQHvtSiAlBF1j5oJ3bSxCp0AKHNzL1YW5YGgF+kzdA=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js"
integrity="sha512-EBLzUL8XLl+va/zAsmXwS7Z2B1F9HUHkZwyS/VKwh3S7T/U0nF4BaU29EP/ZSf6zgiIxYAnKLu6bJ8dqpmX5uw=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
window.addEventListener('load', function () {
hljs.highlightAll();
});
</script>
</body>
</html>