API随机获取一言代码

API随机获取一言代码

<?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

[warning]车到山前必有雾,船到桥头自然沉。[/warning]

 » 本文来自热心的夹友 Niuma 提供。

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索