源代码
<?php
/*===========================================================
= 版权协议:
= GPL (The GNU GENERAL PUBLIC LICENSE Version 2, June 1991)
=------------------------------------------------------------
= 文件名称:english.php
= 摘 要:英文语句显示调用函数 for PHP5
= 版 本:1.0
=------------------------------------------------------------
= Script Written By 56Wan项目组
= 最后更新:night
= 最后日期:2008-10-23
= 官方地址:http://www.56wan.com
=------------------------------------------------------------
= 最后更新:Geticer
= 最后日期:2024-07-21
============================================================*/
$DATA = json_decode(file_get_contents('56wan.json'),true)['56wan_sentence'];
function get_english($num = 1) {
global $DATA;
$num = min(max(64,$num),1);
$result = [];
for ($i = 0; $i < $num; $i++) {
$rnum = mt_rand(0, count($DATA) - 1); // 注意数组索引是从0开始的
$result[] = $DATA[$rnum];
}
return $result;
}
// 处理XHR请求并返回JSON数据
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$numSentences = isset($_GET['num']) ? intval($_GET['num']) : 1;
$englishSentences = get_english($numSentences);
header('Content-Type: application/json');
echo json_encode($englishSentences);
exit;
} else {
die;
}