如何编写一个自己的图片API

吃猫的鱼
2022-02-06 / 0 评论 / 362 阅读 / 正在检测是否收录...

今天在 白衣博客 看到这个有趣的图片api制作,分享给大家。
出处 白衣博客

96331795e60c2ded3aa2c6db68ff2474_15995.png

<?php
//存有链接的文件名
$filename = "此处填写自定义.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"));
}

30fcdd29-a168-4deb-adac-a48246a6beee.png
4471e811-73ba-4c9f-88a4-cd65766c1c99.png


4

评论 (0)

取消
友情链接 文章阅读: 网站地图