使用必应Bing每日图片做网站背景(自动)
侧边栏壁纸
  • 累计撰写 114 篇文章
  • 累计收到 22 条评论

使用必应Bing每日图片做网站背景(自动)

SanLiLin
2019-02-14 / 2 评论 / 2,987 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2019年03月22日,已超过1538天没有更新,若内容或图片失效,请留言反馈。

创建API文件

在项目允许访问的目录下创建一个PHP文件供后续调用 api.php 调用地址:http://访问目录/api.php

API代码

以下代码复制粘贴即可
<?php
//从bing获取数据
$str = file_get_contents('https://cn.bing.com/HPImageArchive.aspx?idx=0&n=1');

if (preg_match("/\<url\>(.+?)<\/url>/ies", $str, $matches)) {
    //正则匹配抓取图片url
    $imgurl = 'https://cn.bing.com' . $matches[1];
} else {
    //使用默认的图像
    $imgurl = 'https: //picsum.photos/1920/1080/?random';
}
header("Location: $imgurl");
?>

使用方法

例:如果作为页面背景图的话,使用下面代码即可
body{
    width:100%;
    height:100%;
    background: url(API地址) no-repeat;
    -moz-background-size: cover; /*背景图片拉伸以铺满全屏*/
    -ms-background-size: cover;
    -webkit-background-size: cover;
    background-size: cover;
}
0

评论 (2)

取消
  1. 头像
    三里林 作者
    Windows 7 · Google Chrome

    来都来了,那就说两句吧

    回复
  2. 头像
    Typecho
    Linux · Google Chrome

    欢迎加入 Typecho 大家族

    回复