一个文件自建BingAPI每日一图

最近到处都看到讨论Bing的每日一图,心想难道这个话题又开始火了?那我们就来自己搭个API自己用就好!


浏览了一圈,发现最近出了个PHP版本的,挺好用,又简单,直接上传即可使用,今天就不放文件,直接上代码,自己建一个index.php文件复制进去就好!

<?php
  $cache_path = ‘bing_cache’;
  $time_file = $cache_path.‘/time.txt’;
  $url_file = $cache_path.‘/url.txt’;
  $copyright_file = $cache_path.‘/copyright.txt’;
  $day = $_GET[‘day’];
  $size = $_GET[‘size’];
  $type = $_GET[‘type’];
  $resolution = $_GET[‘resolution’];
  $url = ‘https://www.bing.com/HPImageArchive.aspx?format=js&idx=’.$day.‘&n=1’;
   
  # 文件/夹不存在就创建
  if(!is_dir($cache_path)){
  mkdir($cache_path,0755);
  }
  if(!is_file($time_file)){
  touch($time_file);
  }
  if(!is_file($url_file)){
  touch($url_file);
  }
  if(!is_file($copyright_file)){
  touch($copyright_file);
  }
   
  # 从’time.txt’读取时间戳,判断和上一次访问的间隔在60秒内就直接从本地’url.txt’读取图片url
  $old_time = intval(file_get_contents($time_file));
  if (time()-$old_time>60){
  $time_file_open = fopen
($time_file,‘w+’);
  fwrite($time_file_open,time());
  fclose($time_file_open);
  $urlbase = get_data(false)[0];
  $copyright = get_data(false)[1];
  }else{
  $urlbase = get_data(true)[0];
  $copyright = get_data(true)[1];
  }
   
  switch ($type){
  # 从$type判断类型,返回
  case ‘text’:
  global $copyright;
  echo $copyright;
  break;
  case ‘url’:
  echo get_url(‘url’);
  break;
  default:
  header(“status: 302”);
  header(“Location: “.get_url(‘pic’));
  }
   
  function get_url($type){
  # 从$size或$resolution判断图片的分辨率
  global $urlbase,$size,$resolution;
  if ($resolution){
  $urlsize = ‘_’.$resolution.‘.jpg’;
  }else{
  switch ($size)
  {
  case ‘2k’:
  case ‘1440’:
  case ‘2560’:
  $urlsize = ‘_UHD.jpg’;
  break;
  case 1920:
  $urlsize = ‘_1920x1080.jpg’;
  break;
  case 1080:
  $urlsize = ‘_1080x1920.jpg’;
  break;
  default:
  $urlsize = ‘_1920x1080.jpg’;
  }
  }
  $imgurl = ‘https://www.bing.com’.$urlbase.$urlsize;
  return $imgurl;
  }
   
  function get_data($islocal){
  # $islocal为true时从本地读取,否则从bing的api请求获得
  global $time_file,$url_file,$copyright_file,$url;
  if ($islocal){
  $urlbase = file_get_contents($url_file);
  $copyright = file_get_contents($copyright_file);
  }else{
  $json = json_decode(file_get_contents($url),true);
  $urlbase = $json[‘images’][0][‘urlbase’];
  $copyright = $json[‘images’][0][‘copyright’];
  $url_file_open = fopen($url_file,‘w+’);
  fwrite($url_file_open,$urlbase);
  fclose($url_file_open);
   
  $copyright_file_open = fopen($copyright_file,‘w+’);
  fwrite($copyright_file_open,$copyright);
  fclose($copyright_file_open);
  }
  return array($urlbase,$copyright);
  }
   
  ?>

 

我们来看看效果:

bingapi - 一个文件自建BingAPI每日一图

参数
day 时间,0是今天,1是前1天,2是前2天,最多前7天。默认今天。
type pic,url,text 分别是直接跳转图像链接,展示链接,展示图片描述(版权)
size 图片大小,2k,1920,1080,分别是2K(也可能4k),1920×1080,1080×1920
resolution 具体的分辨率,会覆盖size参数,具体的分辨率,已知有以下这些
#横屏
1920×1200
1920×1080
1366×768
1280×768
1024×768
800×600
800×480
#竖屏
1080×1920
768×1366
768×1280
720×1280
640×480
480×800
400×240
320×240
240×320
#正方形(可以作为头像之类的)
320×320
240×240

例如:(只显示1080大小的链接)
http://www.yunflare.cn/bingapi/index.php?size=1080&type=url

源码来自:https://github.com/peng4740/BingApi