找回密码
 立即注册

API随机获取图片接口代码

2022-7-23 21:37:50 · 站长社区
 先创建一个文件夹tu和一个名为api.php的文件,然后在tu的文件夹内放图片,把下列代码放在api.php里

  1. <?php
  2. //API名称
  3. $APIname='ACG_API';
  4. // 存储数据的文件
  5. $filename = 'link.txt';
  6. if(!file_exists($filename)) {
  7.     die($filename.'数据文件不存在');
  8. } else {
  9.     //读取资源文件
  10.     $giturlArr = file($filename);
  11. }
  12. $giturlData = [];
  13. //将资源文件写入数组
  14. foreach ($giturlArr as $key => $value) {
  15.     $value = trim($value);
  16.     if (!empty($value)) {
  17.         $giturlData[] = trim($value);
  18.     }
  19. }
  20. //获取随机数
  21. $randKey = rand(0, count($giturlData)-1);
  22. //取链接
  23. $imgurl = $giturlData[$randKey];
  24. $returnType = $_GET['return'];
  25. switch ($returnType) {
  26.     case 'img':
  27.         $img = file_get_contents($imgurl, true);
  28.         header("Content-Type: image/jpeg;");
  29.         echo $img;
  30.         break;
  31.     case 'json':
  32.         $json['API_NAME'] = $APIname;
  33.         $json['imgurl'] = $imgurl;
  34.         $imageInfo = getimagesize($imgurl);
  35.         $json['width'] = $imageInfo[0];
  36.         $json['height'] = $imageInfo[1];
  37.         header('Content-type:text/json');
  38.         echo json_encode($json,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  39.         break;   
  40.     default:
  41.         header("Location:" . $imgurl);
  42.         break;
  43. }
  44. ?>
复制代码
 然后在到同级目录下创建一个txt文档,命名也是随机(如:link.txt),将图片的文件名填入此文档,一行一个。

调用JSON格式输出:https://域名/api.php?return=json
直接输出图片:https://域名/api.php?return=img



全部评论 0

 先创建一个文件夹tu和一个名为api.php的文件,然后在tu的文件夹内放图片,把下列代码放在api.php里

  1. <?php
  2. //API名称
  3. $APIname='ACG_API';
  4. // 存储数据的文件
  5. $filename = 'link.txt';
  6. if(!file_exists($filename)) {
  7.     die($filename.'数据文件不存在');
  8. } else {
  9.     //读取资源文件
  10.     $giturlArr = file($filename);
  11. }
  12. $giturlData = [];
  13. //将资源文件写入数组
  14. foreach ($giturlArr as $key => $value) {
  15.     $value = trim($value);
  16.     if (!empty($value)) {
  17.         $giturlData[] = trim($value);
  18.     }
  19. }
  20. //获取随机数
  21. $randKey = rand(0, count($giturlData)-1);
  22. //取链接
  23. $imgurl = $giturlData[$randKey];
  24. $returnType = $_GET['return'];
  25. switch ($returnType) {
  26.     case 'img':
  27.         $img = file_get_contents($imgurl, true);
  28.         header("Content-Type: image/jpeg;");
  29.         echo $img;
  30.         break;
  31.     case 'json':
  32.         $json['API_NAME'] = $APIname;
  33.         $json['imgurl'] = $imgurl;
  34.         $imageInfo = getimagesize($imgurl);
  35.         $json['width'] = $imageInfo[0];
  36.         $json['height'] = $imageInfo[1];
  37.         header('Content-type:text/json');
  38.         echo json_encode($json,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  39.         break;   
  40.     default:
  41.         header("Location:" . $imgurl);
  42.         break;
  43. }
  44. ?>
复制代码
 然后在到同级目录下创建一个txt文档,命名也是随机(如:link.txt),将图片的文件名填入此文档,一行一个。

调用JSON格式输出:https://域名/api.php?return=json
直接输出图片:https://域名/api.php?return=img



热门推荐
您需要登录后才可以回帖 立即登录
说说你的想法......
0
0
0
返回顶部