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__);
$filename = $path."/img.txt";
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)];
//判断输出格式
if (isset($_GET['type']) && $_GET['type'] === 'json') {
header('Content-type:text/json');
die(json_encode(['pic' => $pic], JSON_UNESCAPED_UNICODE));
} else {
die(header("Location: $pic"));
}
上传你的服务器或者主机空间,命名随意后缀为.php
并在同级目录上传一个为img.txt
后缀的文件
PS:一个图片链接链接占一行
访问形式为 http(s)://你的域名/你的命名.php
例如 https://bsgun.cn/m.php
输出为图片
评论区