PHP单文件Bing必应壁纸api

2021-03-02  阅读 1094 次

带缓存功能,每天只向Bing请求一次,以免访问频率过快被ban掉IP

<?php
header("Content-Type:text/html;charset=UTF-8");
error_reporting(0);
date_default_timezone_set('Asia/Shanghai'); //设置中国时区
//https://yaaibk.com/?day=0&size=1024x768&type=pic

$day=isset($_GET['day'])?$_GET['day']:'0';//0 今天,1 前1天,2 前2天,最多前7天,空为今天
$size=isset($_GET['size'])?$_GET['size']:'';//图片大小,1024x768,1920x1080,1080x1920,空为默认图片大小
$type=isset($_GET['type'])?$_GET['type']:'';//pic直接跳转图像链接,url链接,text图片描述,空为json
$cache_path = 'bing_cache';
$dir=$cache_path.'/'.$day.'_time.txt';

# 文件/夹不存在就创建
if(!is_dir($cache_path)){
    mkdir($cache_path,0755);
}
//if(!is_file($dir)){
//    touch($dir);
//}

$url = 'https://www.bing.com/HPImageArchive.aspx?format=js&idx='.$day.'&n=1';

if(is_file($dir)){
	$json = file_get_contents($dir);
	$array = json_decode($json,true);// JSON字符串转成 数组
	if($array['images'][0]['enddate']==date('Ymd',time())){
		$pic_url='https://www.bing.com'.$array['images'][0]['url'];
		$pic_urlbase='https://www.bing.com'.$array['images'][0]['urlbase'];
		$title=$array['images'][0]['copyright'];
		if($size){
			$pic_url=$pic_urlbase.'_'.$size.'.jpg';
		}
	}else{
		$json = get_web_page($url);
		$array = json_decode($json,true);// JSON字符串转成 数组
		$pic_url='https://www.bing.com'.$array['images'][0]['url'];
		$pic_urlbase='https://www.bing.com'.$array['images'][0]['urlbase'];
		$title=$array['images'][0]['copyright'];
		if($size){
			$pic_url=$pic_urlbase.'_'.$size.'.jpg';
		}
		file_put_contents($dir,$json);
	}
}else{
	$json = get_web_page($url);
	$array = json_decode($json,true);// JSON字符串转成 数组
	$pic_url='https://www.bing.com'.$array['images'][0]['url'];
	$pic_urlbase='https://www.bing.com'.$array['images'][0]['urlbase'];
	$title=$array['images'][0]['copyright'];
	if($size){
		$pic_url=$pic_urlbase.'_'.$size.'.jpg';
	}
	file_put_contents($dir,$json);
}

switch ($type){
    case 'text':
        echo $title;
        break;
    case 'url':
        echo $pic_url;
        break;
    case 'pic':
        header("status: 302");
        header("Location: ".$pic_url);
        break;
    default:
		echo $json;
}

function get_web_page($url){
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     //返回网页
        CURLOPT_HEADER         => false,    //不返回头信息
        CURLOPT_SSL_VERIFYPEER => 0,     //验证对等证书
        CURLOPT_SSL_VERIFYHOST => 2,     //检查服务器SSL证书
        CURLOPT_USERAGENT      => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17 SE 2.X MetaSr 1.0", // 设置UserAgent
        CURLOPT_AUTOREFERER    => true,     //引用页重定向
        CURLOPT_CONNECTTIMEOUT => 120,      //连接超时
        CURLOPT_TIMEOUT        => 120,      //回复超时
    );
    $ch = curl_init($url);
    curl_setopt_array($ch,$options);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}
?>

关于图片大小如下

#横屏
1920x1200
1920x1080
1366x768
1280x768
1024x768
800x600
800x480
#竖屏
1080x1920
768x1366
768x1280
720x1280
640x480
480x800
400x240
320x240
240x320
#正方形(可以作为头像之类的)
320x320
240x240


本文地址:https://yaaibk.com/post/244.html
版权声明:本文为原创文章,版权归 本站 所有,欢迎分享本文,转载请保留出处!

评论已关闭!