Bing美图API源码

文章目录
  1. 1. 一、Bing每日背景图片接口源码
    1. 1.1. 接口简介
    2. 1.2. 接口源码
  2. 2. 二、Bing美图抓取API代码
  3. 3. 三、调用必应Bing每日图片做网站背景/api代码分享
    1. 3.1. 代码
  4. 4. 四、获取Bing每日图片API接口
  5. 5. 五、GitHub 源码
  6. 6. 六、php抓取

一、Bing每日背景图片接口源码

接口简介

获取Bing每日一图接口源码 图片每天会自动更新 一天更新一张 浏览lanyou的新模版 MATA 无意中发现了每日获取Bing接口源码 分享给需要做网站背景的来使用 网站背景代码大部分都会 我这里也不打出来了 接口在线演示:https://api.tx7.co/by.php

接口源码

1
2
3
4
5
6
7
8
9
10
11
<?php
/**
*Bing图片获取
*/
$str = file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1');
if(preg_match("/<url>(.+?)<\/url>/ies",$str,$matches)){ $imgurl='http://cn.bing.com'.$matches[1];
}else{
$imgurl='/content/templates/dudu/img/a.jpg';
}
header("Location: $imgurl");
?>

来自晓超云博客-原文地址:https://www.tx47.cn/xcbk-471.html

二、Bing美图抓取API代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 function XCURL($url,$referer){
$ip = mt_rand(1,255).".".mt_rand(1,255).".".mt_rand(1,255).".".mt_rand(1,255)."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_HTTPHEADER,array("CLIENT-IP".$ip."", "X_FORWARD_FOR:".$ip.""));
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,15);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
$result=curl_exec($ch);
curl_close($ch);
return $result;
}
if(isset($_REQUEST['rand'])&&!empty($_REQUEST['rand'])&&$_REQUEST['rand']=='true'){
$url='http://cn.bing.com/HPImageArchive.aspx?idx='.rand(0,6).'&n=1';
}else{
$url='http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1';
}
$referer=base64_decode('aHR0cHM6Ly93d3cueHNpY28uY24v');
$str=XCURL($url,$referer);
if(preg_match("/(.+?)<\/url>/ies",$str,$matches)){
$imgurl='http://cn.bing.com'.$matches[1];
}
if(isset($_REQUEST['size'])&&!empty($_REQUEST['size'])){
if($_REQUEST['size']=='1920*1080'){
$imgurl=str_replace("1366x768","1920x1080",$imgurl);
}
}
if(isset($_REQUEST['type'])&&!empty($_REQUEST['type'])){
$type=$_REQUEST['type'];
}else{
$type='jpeg';
}
if($imgurl){
header('Content-Type: image/'.$type);
header(base64_decode('Q29weXJpZ2h0OiB3d3cueHNpY28uY24='));
@ob_end_clean();
@readfile($imgurl);
@flush(); @ob_flush();
exit();
}else{
exit('error');
}

原地址:https://www.xsico.cn/cache/bing-img-curl-api.html

三、调用必应Bing每日图片做网站背景/api代码分享

创建个PHP文件 api.php

调用地址:http://域名/api.php

本站之前还分享过本地图片API可看下哦

直通车:https://www.ly522.com/797.html

代码

1
2
3
4
5
6
7
8
 $str = file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1'); //从bing获取数据
if(preg_match("/(.+?)<\/url>/ies",$str,$matches)){ //正则匹配抓取图片url
$imgurl='http://cn.bing.com'.$matches[1];
}else{ //如果由于某些原因,没抓取到图片地址
$imgurl='修改为你自己的图片地址'; //使用默认的图像
}
header("Location: $imgurl"); //header跳转
?>

当成背景使用的话那么CSS应该这样写

1
2
3
4
5
6
7
8
9
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;
}

原地址:https://www.ly522.com/1314.html

四、获取Bing每日图片API接口

bing图片每日更新,对于这一点感觉挺不错的,如果能够把bing每日图片作为博客背景是不是很不错呢?首先我们进入Bing首页,会发现自动转到中国版。不过这没关系,中国版更符合国情,速度也比国际版快一些。
下面分享bing图片接口api,通过抓包,可以发现

1
http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1

这里可以获取到无水印的图片。

抓包显示结果:

1
{"images":[{"startdate":"20160613","fullstartdate":"201606131600","enddate":"20160614","url":"http://s.cn.bing.net/az/hprichbg/rb/BomboHeadland_ZH-CN9339065341_1920x1080.jpg","urlbase":"/az/hprichbg/rb/BomboHeadland_ZH-CN9339065341","copyright":"凯马Bombo Headland,新南威尔士州,澳大利亚 (© Attakorn Bk/Getty Images)","copyrightlink":"http://www.bing.com/search?q=%E5%87%AF%E9%A9%AC&form=hpcapt&mkt=zh-cn","wp":true,"hsh":"8b50b348242b793c63102d33c2ca5eb7","drk":1,"top":1,"bot":1,"hs":[],"msg":[{"title":"今日图片故事","link":"http://www.bing.com/search?q=%E5%87%AF%E9%A9%AC&FORM=pgbar1&mkt=zh-cn","text":"凯马"}]}],"tooltips":{"loading":"正在加载...","previous":"上一个图像","next":"下一个图像","walle":"此图片不能下载用作壁纸。","walls":"下载今日美图。仅限用作桌面壁纸。"}}

我们从中提取图片链接,url地址:
http://s.cn.bing.net/az/hprichbg/rb/BomboHeadland_ZH-CN9339065341_1920x1080.jpg

1
2
3
4
5
6
如果我们想浏览昨天,前天的图片呢?

回到最初的网址上,http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1 在这里,更换 idx= 这个参数就可以了。

如 http://cn.bing.com/HPImageArchive.aspx?idx=1&n=1 将要得到昨天的图片
http://cn.bing.com/HPImageArchive.aspx?idx=2&n=1得到前天的图片

官网bing每日图片api知道教程如下,如果想了解更多内容点击进入——bing图片api用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1. 访问 https://api.ioliu.cn/bing/ , 返回bing每日最新背景图, 可选参数[w,h]
2. 访问 https://api.ioliu.cn/bing/?d=n (n>=0), 返回以当日为起点第n天前的壁纸, 可选参数[w,h]
3. 访问 https://api.ioliu.cn/bing/json/ , 返回bing每日最新壁纸的相关(介绍、图片地址等)信息(json格式), 可选参数[callback].
4. 访问 https://api.ioliu.cn/bing/rand/ , 返回随机图片, 可选参数[w,h]
5. 访问 https://api.ioliu.cn/bing/blur/ , 返回高斯模糊图片, 可选参数[d,w,h]
带[w,h]用法: https://api.ioliu.cn/bing/rand/?w=1920&h=1200

目前已知图片分辨率
1920x1200 1920x1080 1366x768 1280x768
1024x768 800x600 800x480 768x1280
720x1280 640x480 480x800 400x240
320x240 240x320

原网址:http://www.eso360.cn/html/2408963950.html

五、GitHub 源码

GitHub地址:https://github.com/xCss/bing

六、php抓取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$str=file_get_contents('https://cn.bing.com/HPImageArchive.aspx?idx=0&n=1');
if (preg_match("/<url>(.+?)<\/url>/ies", $str, $matches)) {
$imgurl='https://cn.bing.com'.$matches[1];
}
if ($imgurl) {
header('Content-Type: image/JPEG');
@ob_end_clean();
@readfile($imgurl);
@flush();
@ob_flush();
exit();
} else {
exit('error');
}
?>