博客主页 😶
自建一个自用随机图片api
自建一个自用随机图片api

Author:

xuanSAMA

©

Wordage:

共计 878 字

needs:

约 1 分钟

Popular:

243 ℃

Created:

目 录

在网上copy的PHP代码自己改了改,通过GET请求当class = 1或者没带class参数时返回宽图,当class=2时返回长图。
图片链接由本地的txt文件来存放。

<?php
$imgClass = $_GET['class'];
if ($imgClass == null | $imgClass == 1) {
    // class=1代表宽图
    // 1.读取imgurl.txt中的内容,并以换行符分开
    $str = explode("\n", file_get_contents('imgurl.txt'));
}else if($imgClass == 2){
    $str = explode("\n", file_get_contents('img1url.txt'));
}

// 2.得到的$str是一个String的数组,然后获取随机数index
$rand_index = rand(0,count($str)-1);
// 根据生成的随机数选取index为$rand_index的图片链接
$url = $str[$rand_index];
// 替换掉转义
$url = str_re($url);
// 3.重定向到目标url,返回302码,然后浏览器就会跳转到图片url的地址
header("Location:".$url);
// 替换掉一些换行、制表符等转义
function str_re($str){
    $str = str_replace(' ', "", $str);
    $str = str_replace("\n", "", $str);
    $str = str_replace("\t", "", $str);
    $str = str_replace("\r", "", $str);
    return $str;
  }
?>
文章二维码
自建一个自用随机图片api
共计 0 条评论,点此发表评论
博客主页 流光博客 欢迎来到我的博客,我会在这里分享我的学习笔记,纪录一些生活。 51统计
萌ICP备20220869号 本站已运行 1 年 132 天 17 小时 23 分 自豪地使用 Typecho 建站,并搭配 MyDiary 主题 Copyright © 2022 ~ 2024. 流光博客 All rights reserved.
打赏图
打赏博主
欢迎
搜 索
足 迹
分 类
  • 📚 学习生涯
  • 📔 日常纪录
  • 🖥️ 软件分享
  • 🗂️ 杂项
  • 🖼️ 相册