Tonm's Blog

Tonm's Blog



API随机获取图片,接口代码分享

Tonm · 2020-07-27 · 1882浏览 · 代码∷插件


首先先创建一个php,随便命名(如:tonm.php),内容代码如下:

<?php
//API名称
$APIname='ACG_API';
// 存储数据的文件
$filename = 'link.txt';
if(!file_exists($filename)) {
    die($filename.'数据文件不存在');
} else {
    //读取资源文件
    $giturlArr = file($filename);
}
$giturlData = [];
//将资源文件写入数组
foreach ($giturlArr as $key => $value) {
    $value = trim($value);
    if (!empty($value)) {
        $giturlData[] = trim($value);
    }
}
//获取随机数
$randKey = rand(0, count($giturlData)-1);
//取链接
$imgurl = $giturlData[$randKey];
$returnType = $_GET['return'];
switch ($returnType) {
    case 'img':
        $img = file_get_contents($imgurl, true);
        header("Content-Type: image/jpeg;");
        echo $img;
        break;
    case 'json':
        $json['API_NAME'] = $APIname;
        $json['imgurl'] = $imgurl;
        $imageInfo = getimagesize($imgurl);
        $json['width'] = $imageInfo[0];
        $json['height'] = $imageInfo[1];
        header('Content-type:text/json');
        echo json_encode($json,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
        break;    
    default:
        header("Location:" . $imgurl);
        break;
}
?>

然后在到同级目录下创建一个txt文档,命名也是随机(如:link.txt)

调用

JSON格式输出:https://域名/tonm.php?return=json

直接输出图片:https://域名/tonm.php?return=img

PS:一个图片链接链接占一行





comment 评论区

添加新评论





  • ©2025 bilibili.com

textsms
内容不能为空
account_circle
昵称不能为空
email
邮件地址格式错误
web
beach_access
验证码不能为空
keyboard发表评论


star_outline 咱快来抢个沙发吧!




©2025 Tonm's Blog

Theme Romanticism2.1 by Akashi
Powered by Typecho