侧边栏壁纸
博主昵称
荒妖

江水流春去欲尽

api随机图教程

  • 荒妖 丨2023年03月03日 丨2.6w阅读 丨1评论
  • 现在教同志们,一个很简单的又属于自己的api随机图片,

    直接调用

    二次元
    https://api.wukeke.top
    返回示例
    二次元
    电脑版风景
    https://api.wukeke.top/fengjing.php
    返回示例
    PC风景

    手机版风景
    https://api.wukeke.top/jian.php
    返回示例
    手机风景
    以下打包500张图片

    https://ge.wukeke.top/hyhy/index.html

    这里推荐服务器
    雨云 ———— https://www.rainyun.cc/?ref=MzMzNjQ=

    教程开始

    第一步:新建一个文件夹,命名为:huangyao(这个文件里放你需要的图片)后面还要用到这里。

    第二步:新建一个index.php文件,把以下代码填写进去 (这个文件就是api地址)

    <?php
    $huangyao_array = glob("huangyao/*.{webp,gif,jpg,png}",GLOB_BRACE); 
    $huangyao = array_rand($huangyao_array); 
    $dz = $huangyao_array[$huangyao];
    header("Location:".$dz);
    ?>
    文件样式

    第一步:创建一个huangyao.txt文件 (这个文件里放你的储存的图片链接,一行一条)
    第二步:新建一个index.php文件,写入以下代码 (这个文件就是api地址)

    
    <?php
    
    //存有链接的文件名,这里是存放图片链接的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"));
    }
    32赞
    打赏

    —— 评论区 ——

    昵称
    邮箱
    网址
    取消
    1. 头像
      女装品牌前十名
      Windows 10 x64 Edition Windows 10 x64 Edition   Google Chrome 132 Google Chrome 132
      回复

      文章不错支持一下,非常喜欢

    博主栏壁纸
    博主头像 荒妖

    江水流春去欲尽

    51 文章数
    15 标签数
    49 评论量
    人生倒计时
    舔狗日记