前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP/Node.js获取Bing每日壁纸

PHP/Node.js获取Bing每日壁纸

原创
作者头像
Cong Min
修改2019-07-31 12:36:19
2.1K1
修改2019-07-31 12:36:19
举报
文章被收录于专栏:Think & DifferentThink & Different

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


PHP跳转图片地址(推荐)

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

bing.php

代码语言:txt
复制
<?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

代码语言:txt
复制
<?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

代码语言:txt
复制
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);
代码语言:txt
复制
$ node bing.js

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • PHP跳转图片地址(推荐)
  • PHP代理图片
  • Node.js代理图片
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档