欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

PHP/Node.js 获取必应每日壁纸

最编程 2024-07-07 16:00:24
...

Github:https://github.com/mcc108/bing-wallpaper


PHP跳转图片地址(推荐)

效果:http://congm.in/bing.php

bing.php

<?php
    $str = file_get_contents('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1');
    $data = json_decode($str);
    $imghost = 'https://cn.bing.com';
    $imgpath = $data -> {'images'}[0] -> {'url'};
    if ($imgpath) {
        $imgurl = $imghost . $imgpath;
        header('Location:' . $imgurl);
        exit();
    } else {
        exit('error');
    }
?>

PHP代理图片

bing_agent.php

<?php
    $str = file_get_contents('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1');
    $data = json_decode($str);
    $imghost = 'https://cn.bing.com';
    $imgpath = $data -> {'images'}[0] -> {'url'};
    if ($imgpath) {
      $imgurl = $imghost . $imgpath;
        $img = imagecreatefromjpeg($imgurl);
        header('Expires: ' . gmdate('D, d M Y H:i:s', strtotime(date('Y-m-d', strtotime('+1 day')))) . ' GMT');
        header('Cache-Control: public, max-age=3600');
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s', strtotime(date('Y-m-d'))) . ' GMT');
        header('Content-Type: image/jpeg');
        imageinterlace($img, 1);
        imagejpeg($img);
        imagedestroy($img);
    } else {
        exit('error');
    }
?>

Node.js代理图片

bing_agent.js

const http = require('http');
http.createServer((req, response) => {
    const today = new Date();
    today.setHours(0);
    today.setMinutes(0);
    today.setSeconds(0);
    today.setMilliseconds(0);
    const tomorrow = new Date();
    tomorrow.setTime(today.getTime() + (24 * 3600 * 1000));
    response.writeHead(200, {
        'Expires': tomorrow.toUTCString(),
        'Cache-Control': 'public, max-age=3600',
        'Last-Modified': today.toUTCString(),
        'Content-Type': 'image/jpeg'
    });
    http.get('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1', (bing_res) => {
        let bing_body = [], bing_data = {};
        bing_res.on('data', (chunk) => {
            bing_body.push(chunk);
        });
        bing_res.on('end', () => {
            bing_body = Buffer.concat(bing_body);
            bing_data = JSON.parse(bing_body.toString());
            http.get(`https://www.bing.com${bing_data.images[0].url}`, (img_res) => {
                let img_body = [];
                img_res.on('data', (chunk) => {
                    img_body.push(chunk);
                });
                img_res.on('end', () => {
                    img_body = Buffer.concat(img_body);
                    response.write(img_body, 'binary');
                    response.end();
                });
            });
        });
    });
}).listen(8129);
$ node bing.js

利用NodeJS/PHP就可以获取每日更新的Bing壁纸来作网页背景啦:

  • https://congm.in
  • https://i.congm.in