创建typeA.txt、typeB.txt、typeC.txt 文件来存放图片链接,文件名:typeA.txt与api.php同目录下,并填入图片链接
";
/*
获取传入参数、输出图片类型
*/
switch ($_GET["type"]) {
case 'a':$filename = "typeA.txt";/*对应图像类型文件*/
break;
case 'w':$filename = "typeB.txt";/*对应图像类型文件*/
break;
case 't':$filename = "typeC.txt";/*对应图像类型文件*/
break;
default:$filename = "typeA.txt"; /*默认输出图片类型*/
break;
}
/*
判断文件是否存在
*/
if(!file_exists($filename)) {
die('文件不存在');
}
/*
从文本获取链接
*/
$pics = [];
$fs = fopen($filename, "r");
while(!feof($fs)) {
$line=trim(fgets($fs));
if($line!='') {
array_push($pics, $line);
}
}
/*
从数组随机获取链接
*/
$pic = $pics[array_rand($pics)];
/*
返回指定格式
*/
$type=$_GET['text'];
switch($type) {
case 'json':
header('Content-type:text/json');
die(json_encode(['pic'=>$pic],JSON_UNESCAPED_SLASHES));/*JSON返回*/
default:
die(header("Location: $pic"));/*重定向图片链接*/
}
?>
去除api.php的后缀.php需要伪静态:
location / {
if (!-e $request_filename){
rewrite ^(.*)$ $1.php last;
}
}