作者:Smileby陌少羽 | 2018-09-01 10:40 |点击:
请求https://api.xiaolin.in/hitokoto
返回 起风了,必须好好活下去。
脚本地址https://api.xiaolin.in/hitokoto/?code=js
将下面这段代码放入页面内需要展示一句话的位置即可
<p id="test"></p>
<script type="text/javascript" src="https://api.xiaolin.in/hitokoto/?code=js&id=test"></script>
污则欲避,轻则易弃。避则无憾于人,弃则无累于己
刷新试试?会变哦~
(请先输入composer require izniburak/pdox安装依赖库 ((
<?php
require_once __DIR__ . '/vendor/autoload.php';
function get_data()
{
$db = new \Buki\Pdox([
'host' => '127.0.0.1',
'port' => 3306,
'username' => 'username',
'password' => 'password',
'database' => 'database',
]);
$num = rand(1,$db->table('hitokoto')->count('id','total_row')->get()->total_row);
return $db->table('hitokoto')->where('id',$num)->get();
}
if (@mb_convert_case($_GET['charset'],MB_CASE_UPPER) == 'UTF-8') $charset = 'UTF-8'; else $charset = 'GBK';
if (empty($_GET['code'])) $code = 'text'; else $code = $_GET['code'];
switch ($code)
{
case 'js':
$str = "document.getElementById('hitokoto').innerText = '" . trim(get_data()->content) . "';";
break;
default:
$str = trim(get_data()->content);
break;
}
header("Content-type: text/html; charset={$charset}");
if ($charset == 'UTF-8') echo $str; else echo mb_convert_encoding($str,$charset);
?>
数据库结构
create table hitokoto
(
id int auto_increment
primary key,
content longtext null,
score float default '2.5' null,
author text null,
origin text null
);
如果编码错误
用utf8的就调用https://api.xiaolin.in/hitokoto/?code=js
用gbk的就调用https://api.xiaolin.in/hitokoto/?code=js&charset=GBK
词库下载地址:点我