API随机获取一言

API随机获取一言代码

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
<?php
$counter = intval(file_get_contents("counter.dat"));
$_SESSION['#'] = true;
$counter++;
$fp = fopen("counter.dat","w");
fwrite($fp, $counter);
fclose($fp);
?>

<?php
//获取句子文件的绝对路径
$path = dirname(__FILE__);
$file = file($path."binduyan.txt");

//随机读取一行
$arr = mt_rand( 0, count( $file ) - 1 );
$content = trim($file[$arr]);

//编码判断,用于输出相应的响应头部编码
if (isset($_GET['charset']) && !empty($_GET['charset'])) {
$charset = $_GET['charset'];
if (strcasecmp($charset,"gbk") == 0 ) {
$content = mb_convert_encoding($content,'gbk', 'utf-8');
}
} else {
$charset = 'utf-8';
}

//格式化判断,输出js或纯文本
if ($_GET['encode'] === 'js') {
echo "function binduyan(){document.write('" . $content ."');}";
}else if($_GET['encode'] === 'json'){
header('Content-type:text/json');
$content = array('text'=>$content);
echo json_encode($content, JSON_UNESCAPED_UNICODE);
}else {
echo $content;
}

上传你的服务器或者主机空间,命名随意后缀为.php

并在同级目录上传一个为binduyuan.txt后缀的文件

PS:一句话占一行

访问形式为 http(s)://你的域名/你的命名.php

例如 https://bsgun.cn/binduyan.php

车到山前必有雾,船到桥头自然沉。