Bing每日壁纸自动下载[PHP实现]

Bing的每日更新的壁纸都很不错,其实微软有提供图片下载的接口供开发者使用。这里介绍PHP定时下载Bing壁纸的方法

接口地址:https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1  (format=js json格式,其他参数请自行尝试)

{
  "images": Array[1][
    {
      "startdate": "20180606",
      "fullstartdate": "201806060700",
      "enddate": "20180607",
      "url": "/az/hprichbg/rb/FlyinDrivein_EN-CN11097970692_1920x1080.jpg",
      "urlbase": "/az/hprichbg/rb/FlyinDrivein_EN-CN11097970692",
      "copyright": "Customers arriving at the Fly-In Drive-in Theater in Wall Township, New Jersey (© Martha Holmes/Time Life Pictures/Getty Images)",
      "copyrightlink": "http://www.bing.com/search?q=first+drive-in+theater&form=hpcapt&filters=HpDate:%2220180606_0700%22",
      "quiz": "/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20180606_FlyinDrivein%22&FORM=HPQUIZ",
      "wp": false,
      "hsh": "3a6848eb28d50907667976c80e6014da",
      "drk": 1,
      "top": 1,
      "bot": 1,
      "hs": Array[0][
        
      ]
    }
  ],
  "tooltips": {
    "loading": "Loading...",
    "previous": "Previous image",
    "next": "Next image",
    "walle": "This image is not available to download as wallpaper.",
    "walls": "Download this image. Use of this image is restricted to wallpaper only."
  }
}

images下的url字段即为图片路径,拼上www.bing.com的前缀就是当日的图片地址了。然后就可以使用图片到各种用途。

这里demo为下载图片到本地,将以下代码保存为.php文件,添加crontab任务,即可定时下载。

<?php
/**
 *          Bing Images Auto Download
 *
 * Copyright 2017 http://yimis.com
 * Create by:   yimis <[email protected]>
 * Date:    11/14/2017
 * Time:    4:58 PM
 *
 * #Bing Images Auto Download
 * 15 08 * * * php -f /data/crontab/bing_download.php
 *
 *
 */

header("Content-type: text/html; charset=utf-8");
date_default_timezone_set("Asia/Shanghai");
$down_path = "/data/bing_img/" . date("Y/");
$down_url_head = "http://www.bing.com";
$url = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1";
$json_arr = getJson($url);
$url_arr = explode("/", $json_arr['images'][0]['url']);
$down_url = $down_url_head . $json_arr['images'][0]['url'];
$pic_name = $url_arr[count($url_arr) - 1];
if (is_file($down_path . $pic_name)) {
    echo json_encode(array('time' => date("Y-m-d H:i:s"), 'file' => $pic_name, 'status' => 'failure', 'msg' => 'already exist'));
    exit;
}
//下载图片
$res = downloadPic($down_url, $down_path, $pic_name);
//写入信息
$info_text = $pic_name . PHP_EOL . "-----------------------" . PHP_EOL . "Time:" . date("Y-m-d H:i:s") . PHP_EOL . "Data:" . serialize($json_arr['images'][0]);
file_put_contents($down_path . "info.txt", $info_text . PHP_EOL . PHP_EOL, FILE_APPEND);
echo json_encode(array('time' => date("Y-m-d H:i:s"), 'file' => $pic_name, 'status' => 'succeed'));

/**
 * 去获取 bing 数据
 * @param $url
 * @return mixed
 */
function getJson($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output, true);
}

/**
 * 下载图片
 * @param $url
 * @param $path
 * @param $name
 * @return string
 */
function downloadPic($url, $path, $name)
{
    if (!is_dir($path)) {
        mkdir($path, 0777, true);
    }
    $content = file_get_contents($url);
    if (file_put_contents($path . $name, $content))
        return $path . $name;
}

?>

 

Leave A Comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked