随机图片API接口 开源

随机图片api


请求说明

请求示例  https://域名/api.php?type=json

请求方式  
GET

返回格式  JSON 返回示例

{ 
"success":true,"url";
 "url":"https:\/\/p2.a.yximgs.com\/ufile\/atlas\/NTIwNTU5ODI4OTEyMzg4ODEwNF8xNjQ3NTMwMTg5MDM3_3.jpg"
 }

 

请求参数

参数名称 是否必需 参数说明
type JSON格式输出

返回参数
参数名称 参数类型 参数说明
success string true
url string 图片链接

更新时间

更新时间:2023-03-20 00:39:10

 

API源码

<?php
// 获取所有图片链接
$image_urls = file('img.txt', FILE_IGNORE_NEW_LINES);

// 获取当前用户的IP地址
$user_ip = $_SERVER['REMOTE_ADDR'];

// 记录IP访问和单个IP访问次数
$ip_counts = json_decode(file_get_contents('ip.txt'), true);
if (!isset($ip_counts[$user_ip])) {
    $ip_counts[$user_ip] = 0;
}
$ip_counts[$user_ip]++;

// 将访问次数写回文件
file_put_contents('ip.txt', json_encode($ip_counts));

// 使用session来记录每个用户的访问次数
session_start();
if (!isset($_SESSION['image_index'][$user_ip])) {
    $_SESSION['image_index'][$user_ip] = 0;
}
$image_index = $_SESSION['image_index'][$user_ip];

// 获取当前链接,并增加用户访问次数
$image_url = $image_urls[$image_index];
$_SESSION['image_index'][$user_ip]++;
if ($_SESSION['image_index'][$user_ip] >= count($image_urls)) {
    $_SESSION['image_index'][$user_ip] = 0;
}

// 判断请求参数,如果为json则返回json格式,否则直接输出图片
if (isset($_GET['type']) && $_GET['type'] === 'json') {
    $response = [
        'success' => true,
        'url' => $image_url
    ];
    header('Content-Type: application/json');
    echo json_encode($response);
} else {
    header('Content-Type: image/jpeg');
    readfile($image_url);
}
?>

在根目录创建一个api.php和img.txt把代码粘贴进api.php里把图片链接放在img.txt里每个链接用换行隔开

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情图片

    暂无评论内容