极简版自建一言语句php
查看效果:https://www.krjojo.com/resources/sentences/
这是hitokoto原版效果:https://v1.hitokoto.cn/
简直就是一模一样。
优点:
文件都在本地服务器
不依赖别人网站,高稳定性
不依赖数据库
可以自己修改语句包
缺点:
- 性能有那么一丢丢下降
一言数据来自:https://github.com/hitokoto-osc/sentences-bundle
一言开源社区官方提供的语句库,系 hitokoto.cn 数据打包集合。
2024年07月31号更新:
新增一言整合进wordpress框架
第一步
下载 语句库 全部 json
文件。
1├── a.json
2├── b.json
3├── c.json
4├── d.json
5├── e.json
6├── f.json
7├── g.json
8├── h.json
9├── i.json
10├── j.json
11├── k.json
12└── l.json
第二步
新建index.php
文件:
1<?php
2// https://v1.hitokoto.cn/
3
4$type_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'];
5if (isset($_GET['c']) && !empty($_GET['c']) && in_array($_GET['c'], $type_list)) {
6 $file = $_GET['c'] . ".json";
7} else {
8 $file = "all.json";
9}
10
11// 读取 JSON 文件内容
12$jsonContent = file_get_contents('./' . $file);
13
14// 解析 JSON 内容为数组
15$array = json_decode($jsonContent, true);
16
17// 从数组中随机选择一段文本
18$randomText = $array[array_rand($array)];
19
20// 设置响应头
21header('Content-Type: application/json');
22
23// 输出随机选中的文本
24echo json_encode($randomText);
25
26?>
第三步
把所有 json
文件合并成一个 all.json
文件,当然我也整理了。
可以直接下载。
https://www.krjojo.com/resources/sentences/all.json
第四步
最后新建一个文件夹把所有文件包起来,文件名字随意,放在站点根目录。
目录下应该为:
1新建文件夹的名字
2├── a.json
3├── all.json
4├── b.json
5├── c.json
6├── d.json
7├── e.json
8├── f.json
9├── g.json
10├── h.json
11├── i.json
12├── index.php
13├── j.json
14├── k.json
15└── l.json
最后访问你的站点: www.example.com/新建文件夹的名字
如果不能正常访问则试试: www.example.com/新建文件夹的名字/index.php
说明
请求参数支持句子类型,与一言官方一致,不传则默认全部类型,如:
1example.com/?c=a
句子类型(参数)
参数 | 说明 |
---|---|
a | 动画 |
b | 漫画 |
c | 游戏 |
d | 文学 |
e | 原创 |
f | 来自网络 |
g | 其他 |
h | 影视 |
i | 诗词 |
j | 网易云 |
k | 哲学 |
l | 抖机灵 |
其他 | 作为 动画 类型处理 |
完毕
一言整合进wordpress框架
看看效果
https://www.krjojo.com/wp-json/krjojo/sentences
优点:可以在后台管理一言了
缺点:性能可能再次下降一点点(都用wp了,谁在意 ¯\(ツ)/¯ )
wordpress加入以下代码,用来注册新的文章类目
1add_action('rest_api_init', function () {
2 register_rest_route('krjojo', 'sentences', [
3 'methods' => 'GET',
4 'callback' => function ($request) {
5 $post = get_posts(['numberposts' => 1, 'post_type' => 'sentences', 'orderby' => 'rand', 'post_status' => 'publish']);
6 $post = $post[0];
7 return [
8 'id' => $post->ID,
9 'hitokoto' => $post->post_title,
10 'from' => $post->post_content,
11 'creator' => $post->post_excerpt,
12 ];
13 },
14 'permission_callback' => function () {
15 return true;
16 }
17 ], 1);
18});
19
20add_action('init', function () {
21 register_post_type('sentences', [
22 'label' => "一言",
23 'labels' => [
24 'add_new' => '写一言'
25 ],
26 'description' => '一言语句',
27 // 'map_meta_cap'=>true,
28 'public' => true,
29 'menu_position' => 5,
30 'supports' => ['title', 'editor', 'comments', 'excerpt'],
31 // 'has_archive' => true
32 ]);
33
34 register_taxonomy('sentences_category', 'sentences', [
35 'labels' => [
36 'name' => _x('一言分类', 'taxonomy 名称'),
37 'singular_name' => _x('一言分类', 'taxonomy 单数名称'),
38 'search_items' => __('搜索一言分类'),
39 'all_items' => __('所有一言分类'),
40 'parent_item' => __('该一言分类的上级分类'),
41 'parent_item_colon' => __('该一言分类的上级分类:'),
42 'edit_item' => __('编辑一言分类'),
43 'update_item' => __('更新一言分类'),
44 'add_new_item' => __('添加新的一言分类'),
45 'new_item_name' => __('新一言分类'),
46 'menu_name' => __('一言分类'),
47 ],
48 'hierarchical' => true,
49 ]);
50});
然后把整个一言导入数据库
post_content 字段对应 from
post_title 字段对应 hitokoto
creator 字段对应 post_excerpt
post_type 字段设置成 sentences
post_status 字段设置成 publish
就可以实现简单的今日一言小组件了
小组件参考代码
为了方便用的是旧版小组件,块组件制作太麻烦了
请根据自己主题样式微调
1//一言一句话
2class Krjojo_Tool_Sentences_Widget extends WP_Widget
3{
4
5 public function __construct()
6 {
7 parent::__construct(
8 'krjojo_tool_sentences_widget',
9 '一言一句话',
10 );
11 }
12
13 function widget($args, $instance)
14 {
15 echo $args['before_widget'];
16?>
17 <div>
18 <strong style="margin-bottom: 15px;"><?php _e('Sentences', 'WP-krjojo-tool') ?></strong>
19 <hr>
20 <div id="krjojo_sentences1">
21 <?php _e('Loading', 'WP-krjojo-tool') ?>...
22 </div>
23 <div class="krjojo_sentences2">-「<span id="krjojo_sentences2"></span>」</div>
24 </div>
25 <script>
26 // 使用 Fetch API 发起 GET 请求
27 let krjojo_sentences1 = document.getElementById("krjojo_sentences1");
28 let krjojo_sentences2 = document.getElementById("krjojo_sentences2");
29 fetch('https://www.krjojo.com/wp-json/krjojo/sentences')
30 .then(response => response.json())
31 .then(data => {
32 krjojo_sentences1.innerHTML = data.hitokoto;
33 krjojo_sentences2.innerHTML = data.from;
34 })
35 .catch(error => console.error('Error fetching data:', error));
36 </script>
37 <style>
38 .krjojo_sentences2 {
39 text-align: right;
40 font-size: 15px;
41 margin-top: 6px;
42 color: #6c757d;
43 }
44 </style>
45<?php
46 echo $args['after_widget'];
47 }
48}
49
50add_action('widgets_init', function () {
51 register_widget('Krjojo_Tool_Sentences_Widget');
52});