随机图片API

注意
本文最后更新于 2024-01-04,文中内容可能已过时。

搭建自己的随机图片API

index.php写入以下代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
//存有美图链接的文件名img.txt
$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){
 
//JSON返回
case 'json':
    header('Content-type:text/json');
    die(json_encode(['pic'=>$pic]));
 
default:
    die(header("Location: $pic"));
}
 
?>
https://cdn.ftls.xyz/images/2021/12/20201224135055.png
大概这样

创建api.你的域名网站并且设置域名解析。在网站根目录下创建文件夹。将上述两个文件放到刚创建的文件夹中。

如你的域名为abc.com,创建的网站为api.abc.com,创建的文件夹为pic。在浏览器中输入api.abc.com/acg/。即可随机显示img.txt中链接指向的图片。

如果你对API调用的图片并没有什么要求可以试试以下内容:

https://api.blogbig.cn/random/
来源:我爱浩瀚 https://www.blogbig.cn/

随机二次元接口源码-双版本https://www.toubiec.cn/99.html

源码版权:苏晓晴

相关API接口Github上有开源:https://github.com/Suxiaoqinx/acgimgurl
作者博客:晓晴博客
随机二次元图片接口-开放回归:https://www.toubiec.cn/318.html

MORE:
15个随机图片API - zaxtyson https://www.cnblogs.com/zaxtyson/p/11628746.html
随机图片获取api - 龙谷情Sinoam https://www.cnblogs.com/lgqrlchinese/p/11386857.html