RandomImg/index.php
2021-04-10 20:21:36 +08:00

27 lines
738 B
PHP

<?php
error_reporting(0);
$file_path = "data/images.json";
$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
$json = $memcache->get('randUrlJson');
// echo "获取缓存";
if(trim($json) == "" || $json == false){
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));
$memcache->set('randUrlJson', $str);
$json = $str;
// echo "设置缓存";
}
}
$res = json_decode($json,true);
$randNum = rand(0,sizeof($res)-1);
$url = $res[$randNum]['url'];
$name = $res[$randNum]['name'];
if($_GET['return'] == 'json'){
$arr = ['code' => 0,'url' => $url,'name' => $name];
echo json_encode($arr);
} else {
// echo $url;
header('Location:'.$url);
}