最近在学php所以边学边做了个php一言和随机图的php接口,没用数据库,用的txt储存的,下面贴代码
代码
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <?php header('Content-type:text/json'); header('Access-Control-Allow-Origin:*'); header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE"); header('Access-Control-Allow-Headers:x-requested-with,content-type');
if(is_array($_GET)&&count($_GET)>0){ if(isset($_GET["word"])){ if($_GET["word"]==1){ $a=file('dmyxyy.txt'); $data = file_get_contents('dmyxyy.txt'); $arr = explode("\n", $data); $rand = array_rand($arr,1); $rnd_line=$a[$rand]; $sucu = explode('-',$rnd_line); $sucu_cc=str_replace("\r\n","",$sucu[1]); $result = array( 'Statuscode'=>'10000', 'State'=>'成功', 'data'=>$sucu[0], 'Source'=>$sucu_cc, ); echo json_encode($result,JSON_UNESCAPED_UNICODE); }else if($_GET["word"]==2){ $a=file('shrcyy.txt'); $data = file_get_contents('shrcyy.txt'); $arr = explode("\n", $data); $rand = array_rand($arr,1); $rnd_line=$a[$rand]; $sucu = explode('-',$rnd_line); $sucu_cc=str_replace("\r\n","",$sucu[1]); $result = array( 'Statuscode'=>'10000', 'State'=>'成功', 'data'=>$sucu[0], 'Source'=>$sucu_cc, ); echo json_encode($result,JSON_UNESCAPED_UNICODE); }else{ $result = array( 'Statuscode'=>'404', 'State'=>'参数不正确', ); echo json_encode($result,JSON_UNESCAPED_UNICODE); } } }else{ $a=file('sjyt.txt'); $data = file_get_contents('sjyt.txt'); $arr = explode("\n", $data); $rand = array_rand($arr,1); $rnd_line=$a[$rand]; $rnd_line=str_replace("\r\n","",$rnd_line); $result = array( 'Statuscode'=>'10000', 'State'=>'成功', 'data'=>$rnd_line, ); echo json_encode($result,JSON_UNESCAPED_UNICODE); } ?>
|
随机图片参数
接口链接: clover.bylds.cn
示例: http://clover.bylds.cn
返回json数据
{“Statuscode”:”10000”,”State”:”成功”,”data”:”https://s1.ax1x.com/2020/04/26/Jg8znJ.jpg\n”}
随机一言参数
接口链接: clover.bylds.cn
参数名字: word #必填
参数属性: 1或2 #选填
1是游戏动漫小说类一言
2是生活日常类一言
示例: http://clover.bylds.cn/?word=1
返回json数据:
{“Statuscode”:”10000”,”State”:”成功”,”data”:”藏好自己,做好清理。”,”Source”:” —— 刘慈欣 《三体》”}