自己建一个随机壁纸API库

首先我们需要建立两个文件:

random.php 和 random.txt

在random.php 中放入以下代码:

<?php

//存有链接的文件名

$filename = "random.txt";



//从文本获取链接

$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"));

}

在random.txt中 放入壁纸图片url地址

如:

https://tva4.sinaimg.cn/large/9bd9b167gy1g4lhipwa7cj21hc0u0ar5.jpg
https://tva2.sinaimg.cn/large/9bd9b167gy1fwsgdypprdj21hc0u0b29.jpg
https://tva4.sinaimg.cn/large/9bd9b167gy1g4lhi0pruej21hc0xc7ix.jpg
https://tva2.sinaimg.cn/large/9bd9b167gy1g2ql6iaiunj21hc0u07wh.jpg
https://tva2.sinaimg.cn/large/9bd9b167gy1fwrt0fwlh3j21hc0u0alc.jpg
https://tva2.sinaimg.cn/large/9bd9b167gy1g4c8thyk18j21hc0u04qp.jpg
https://tva1.sinaimg.cn/large/9bd9b167gy1g2qknf55gpj21hc0u0e6o.jpg
https://tva3.sinaimg.cn/large/9bd9b167ly1g2qks3sxqbj21hc0u01kx.jpg

显而易见,当访问random.php的时候,会自动跳转到random.txt中的随机地址

这里面的地址你可以指向到本地,也可以是一些图床图片!

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 共2条

请登录后发表评论

    • 头像清扬0