新建站点

image-20230320201905510

填写好域名,并选择php版本

编写php

api.php文件如下:

<?php
// 默认是一个404图片
$rst = "https://一张默认的404图片.png";
if(isset($_GET['type'])){
	$type = htmlspecialchars($_GET['type']);
	$file_path = "data.json";
	if (file_exists($file_path)) {
		$file_data = file_get_contents($file_path);
		if($file_data != null){
			$data = json_decode($file_data, true);
			if ($data != null) {
				if (isset($data[$type])) {
					$num = count($data[$type]);
					$index = mt_rand(0, $num-1);
					$rst = $data[$type][$index];
				}
			}
		}	
	}
}
header("Location:".$rst);
exit();
?>

data.json文件格式如下:

{
    "类别1": ["https://图片1.jpg", "https://图片2.png"],
    "类别2": []
}

上传

 将上述两个文件上传到网站目录

image-20230320202218401

访问

https://你的域名/api.php?type=类别

测试

 比如下面这张图片就是随机api的图片

随机图片