自建一言API

<?php
$filename = 'data.txt';
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;
?>

data.txt 里面贴入要随机显示出来的文本,一行一条 。

如果要在静态页面中引用,只要将最后那一行的

echo $result; 

换成

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

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

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