1.生成图床token:
SSH版:
登录图床所在服务器,输入
curl -X POST -F "email=your_email@address" -F "password=your_passwd" https://your.domain/api/v1/tokens
修改文件版:打开Lsky安装目录,在适当的位置添加如下代码(建议添加于第11行</div>后)
vi /resources/views/common/api.blade.php
#原作者链接:https://www.fuuuy.cn/archives/897.html
<div>
<p class="text-lg text-gray-700 font-semibold">Tonken获取</p>
<script src="//lib.baomitu.com/jquery/1.12.4/jquery.min.js"></script>
<form id="token" action="{{ request()->getSchemeAndHttpHost() }}/api/v1/tokens" method="POST">
<div class="my-2 text-sm">
<div class="form-group qqlogin" style="display: none;">
<div class="input-group-addon">邮箱</div>
<input type="email" id="email" name="email" value="{{ Auth::user()->email }}">
</div>
<div style="display: inline-flex;position: relative;">
<div class="px-4 py-3 text-right sm:px-6" style="color: #555;background-color: #eee;border: 1px solid #ccc;">密码</div>
<input type="password" id="password" name="password" placeholder="输入你的密码">
<a href="javascript:;" class="button px-4 py-3 sm:px-6" style="color: #fff;background-color: #337ab7;border-color: #2e6da4;margin-left:10px;">
<div>点击获取</div>
</a>
</div>
<div class="list-group">
<x-code>
<span style="color:tomato;user-select: none;">token:</span><span id="tokenCode"></span>
</x-code>
</div>
</form>
<script>
$(document).ready(function() {
$("#token .button").click(function() {
var url = $("#token").attr("action");
var email = $("#email").val();
var password = $("#password").val();
$.ajax({
type: 'post',
url: url,
data: {
email: email,
password: password
},
success: function(data) {
if (data.status == true) {
$("#tokenCode").html('Bearer ' + data.data.token)
} else {
if (data.message == "password 不能为空。") {
$("#tokenCode").html("密码不能为空!")
} else if (data.message == "The email address or password is incorrect.") {
$("#tokenCode").html("请确认密码是否正确!")
}
}
},
error: function() {
$("#tokenCode").html("请求过于频繁,请稍后再试!")
}
});
});
});
</script>
</div>
重启服务后登录网站后台,在接口处输入账号密码以获取token
2.通过token查询图床相册ID
vi id.py
#Author:张时贰(zhsher.cn)
import json
import requests
token = 'Bearer 1|WtysSm3gxr6' # token,Bearer+空格+token
url = 'https://xx.xx/api/v1/albums' # 图床地址 域名/api/v1/albums
headers = {
"Content-Type": "multipart/form-data",
"Authorization": token,
"Accept": "application/json",
}
if __name__ == "__main__":
r = requests.get ( url, headers=headers )
r.encoding = "utf-8"
getJson = r.json ()[ 'data' ][ 'data' ]
photoID = [ ]
for i in getJson:
tmp = {'id': i[ 'id' ],
'name': i[ 'name' ]}
photoID.append ( tmp )
# print ( json.dumps(photoID,indent=4, ensure_ascii=False) )
for i in photoID:
print ( i )
python3 id.py
3.在网站服务器目录中建立可访问的php文件
vi random.php
<?php
// 授权令牌,输入获取到的图床token
$token = "Bearer 1|WtyA6";
// 特定相册ID
$albumId = 1; // 替换成实际的相册ID
$ttl = 604800;
/**
* 获取指定页码的图片列表
*
* @param string $token 授权令牌
* @param int $albumId 相册ID
* @param int $page 页码
* @return array 图片列表数组
*/
function getImageListByPage($token, $albumId, $page)
{
// 获取特定相册的图片列表,注意修改为图床域名
$url = "https://img.xxxx.xx/api/v1/images?album_id=" . $albumId . "&page=" . $page;
$headers = array(
"Authorization: " . $token,
"Accept: application/json"
);
// 发起请求获取图片数据
$response = file_get_contents($url, false, stream_context_create(['http' => ['header' => implode("\r\n", $headers)]]));
$result = json_decode($response, true);
if ($result["status"] && isset($result["data"]) && !empty($result["data"])) {
return $result["data"];
} else {
return array();
}
}
/**
* 获取随机图片URL
*
* @param string $token 授权令牌
* @param int $albumId 相册ID
* @param int $ttl ttl
* @return string|null 随机图片URL,如果无法获取则返回null
*/
function getRandomImageUrl($token, $albumId, $ttl)
{
// 获取图片列表的总页数
$lastPage = 1;
$currentPage = 1;
$imageUrls = array();
while ($currentPage <= $lastPage) {
// 获取指定页码的图片列表
$imageList = getImageListByPage($token, $albumId, $currentPage);
if (isset($imageList["last_page"])) {
$lastPage = $imageList["last_page"];
}
if (isset($imageList["data"]) && !empty($imageList["data"])) {
foreach ($imageList["data"] as $image) {
// 将图片的URL存储到数组中
$imageUrls[] = $image["links"]["url"];
}
}
// 更新当前页码
$currentPage++;
}
if (!empty($imageUrls)) {
// 从图片URL列表中随机选择一张图片的URL
$randomImageUrl = $imageUrls[array_rand($imageUrls)];
return $randomImageUrl;
}
return null;
}
// 获取随机图片URL
$imageUrl = getRandomImageUrl($token, $albumId, $ttl);
if ($imageUrl) {
// 发送302重定向响应
header("Location: " . $imageUrl);
exit;
} else {
echo "null";
}
// 基于https://zhz.moe/121/ 文章提供的代码进行修改,减去了apcu功能
在浏览器中访问php文件即可生成随机图片
Comments NOTHING