1. 创建站点

在宝塔面板的网站中添加站点作为API地址,将网站开启PHP,并申请一个SSL认证并打开强制https

2. 创建图片链接

打开这个站点的目录,创建一个img.txt和一个index.php

在PicGo的相册中全部选中并复制图片url,将链接粘贴到img.txt中

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
<?php
//存有image链接的文件名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"));
}
?>

参考如下

创建自己的随机图片api