API随机获取图片代码
<?php
$counter = intval(file_get_contents("counter.dat"));
$_SESSION['#'] = true;
$counter++;
$fp = fopen("counter.dat","w");
fwrite($fp, $counter);
fclose($fp);
?>
<?php
$filename = "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)];
$type=$_GET['type'];
switch($type){
case 'json':
header('Content-type:text/json');
die(json_encode(['pic'=>$pic]));
default:
die(header("Location: $pic"));
}
上传你的服务器或者主机空间,命名随意后缀为.php
并在同级目录上传一个为img.txt后缀的文件
PS:一个图片链接链接占一行
访问形式为 http(s)://你的域名/你的命名.php
例如 https://bsgun.cn/m.php
输出为图片
» 标题: API随机获取图片代码» 本文来自热心的夹友 Niuma 提供。