TypechoJoeTheme

尘遇 logo

当我遇上你
排行榜
统计

PHP简单实现一言 / 随机一句功能

醉书生站长
1周前
0 评论
22 阅读
检测收录...
19:58

很多网站都喜欢在页面中加个一言,不过一般都是调用的第三方api。其实,使用万能的php能通过短短的几行代码就实现该功能!

准备工作

首先准备一个代码编辑器,然后新建一个 php 文件,命名为 api.php,再新建一个文件,命名为 jcyulu.dat(两个文件均需采用 UTF-8 编码,否则会乱码)。

打开 jcyulu.dat,在里面贴入要随机显示出来的文本,一行一条。如果你暂时想不出什么比较好的句子的话,害羞我这儿准备了几十条古诗名句,可以直接点我下载备用。
链接:https://share.weiyun.com/3KlCcIIq 密码:eytf6t

上代码

将下列代码复制并粘贴到 api.php 中保存,你的专属“一言” API 就搭建完成了!超简单是不是……

<?php
// 存储数据的文件
$filename = 'jcyulu.dat';        
 
// 指定页面编码
header('Content-type: text/html; charset=utf-8');
 
if(!file_exists($filename)) {
    die($filename . ' 数据文件不存在');
}
 
$data = array();
 
// 打开文档
$fh = fopen($filename, 'r');
 
// 逐行读取并存入数组中
while (!feof($fh)) {
    $data[] = fgets($fh);
}
 
// 关闭文档
fclose($fh);
 
// 随机获取一行索引
$result = $data[array_rand($data)];
 
echo $result;

上面的代码是用 fopen + fgets 函数实现的,有些朋友似乎不是特别喜欢,觉得“效率低”。别急,这儿还有个 file_get_contents 函数实现的版本:

<?php
// 存储数据的文件
$filename = 'jcyulu.dat';        
 
// 指定页面编码
header('Content-type: text/html; charset=utf-8');
 
if(!file_exists($filename)) {
    die($filename . ' 数据文件不存在');
}
 
// 读取整个数据文件
$data = file_get_contents($filename);
 
// 按换行符分割成数组
$data = explode(PHP_EOL, $data);
 
// 随机获取一行索引
$result = $data[array_rand($data)];
 
// 去除多余的换行符(保险起见)
$result = str_replace(array("\r","\n","\r\n"), '', $result);
 
echo $result;

如何在静态页面中引用

上面的代码都是直接在页面中就输出了随机的句子。如果想像一言那样在静态的网页中引用这个 api,该如何实现呢?

很简单,将最后那一行的 echo $result; 换成

echo 'document.write("'.htmlspecialchars($result).'");';

然后在需要的位置用引入 js 的方式进行调用即可。

示例代码:

<script src="http://你的网址.cn/api.php"></script>

下面的这个“随机古诗名句”就是按照上述方法进行的调用:

(↑↑↑ 刷新一下页面,会有不同的句子)

Txt文本版

<?php
/**
 * Wordpress集成一言(Hitokoto)API经典语句功能
 */
// 获取句子文件的绝对路径
// 如果你介意别人可能会拖走这个文本,可以把文件名自定义一下,或者通过Nginx禁止拉取也行。
$path = dirname(__FILE__);
$file = file($path."/jcyulu.txt");
# 随机读取一行
$arr  = mt_rand( 0, count( $file ) - 1 );
$content  = trim($file[$arr]);
# 编码判断,用于输出相应的响应头部编码
if (isset($_GET['charset']) && !emptyempty($_GET['charset'])) {
    $charset = $_GET['charset'];
    if (strcasecmp($charset,"gbk") == 0 ) {
        $content = mb_convert_encoding($content,'gbk', 'utf-8');
    }
} else {
    $charset = 'utf-8';
}
header("Content-Type: text/html; charset=$charset");
# 格式化判断,输出js或纯文本
if ($_GET['format'] === 'js') {
    echo "function hitokoto(){document.write(‘" . $content ."‘);}";
} else {
    echo $content;
}

$file = file($path."/jcyulu.txt"); 这里txt为语录地址,一行一条!或把上面下载的jcyulu.dat改成jcyulu.txt即可。

随机图片

一言有了,图片也不能落下,上菜!
PHP 实现随机图像功能

本文共626个字数,您已阅读:平均阅读时长≈2分钟。
php
朗读
点赞
0
支持
海报
欢迎
要时间嘉许,等春风得意,我正好遇上你!

微信/QQ/支付宝三合一收款码

微信
QQ
支付宝
打开微信/QQ/支付宝扫一扫,即可进行扫码打赏!
版权属于:

尘遇

最后修改:

2021 年 05 月 03 日 08 : 24 PM

本文链接:

https://chenyu.me/1370.html(转载时请注明本文出处及文章链接)

暂无评论

光阴者,百代之过客

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

最新回复

  1. kzkLv.1
    2021-05-10

    {!{data:image/webp;base64,UklGRi4SAABXRUJQVlA4WAoAAAAQAAAAAQMAKwEAQUxQSDUQAAAB8ID//yol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7l/Xn07aHxe5aVXw1kRXgZ+ZRzp2yGy6d8/Sbl/WHT+cGLttXfiyRX4zJnqMHtK/h2O7Fg1qVCxZUOB7Nn7TJjU602iDfCaMH+HSp4ttmyZ1qpQtmlew+m9W/7H6j9V/rP5j9R+r/1j9x+o/Vv/h1Vj9RwAAVlA4INIBAABQNQCdASoCAywBP3G42WW0ryunIAgCkC4JaW7hd2EbQAnsA99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snCgAA/v+tHgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA}!}

  2. 遇上你Lv.1
    2021-05-08
  3. AlanineLv.1
    2021-05-04
  4. 曾经Lv.1
    2021-05-04
  5. 郎君Lv.1
    2021-05-04