PHP搭建一个随机头像获取Api

教程

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

<?php
$img_array = glob("tximg/*.{gif,jpg,png}",GLOB_BRACE); 
$img = array_rand($img_array); 
$dz = $img_array[$img];
header("Location:".$dz);
?>

最后访问地址:http://域名/api.php就可以啦!

外链随机图片API

先创建tximg.txt和api.php文件 

然后在tximg.txt文档内放图片外链地址,在api.php加入下列代码

<?php
$arr=file('tximg.txt');
$n=count($arr)-1;
for ($i=1;$i<=1;$i++){
  $x=rand(0,$n);
  header("Location:".$arr[$x],"\n");
}
?>

最后访问地址:http://域名/api.php即可!

演示站

https://www.ityinhu.com/api/tx/api.php