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

创建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;
}
posted @ 2020-12-16 15:05  三里林  阅读(949)  评论(0编辑  收藏  举报