hank9999小站

PHP获取Bing今日美图

hank9999's Avatar 2019-12-28

  1. 1. 从网上找
  2. 2. 自己写
    1. 2.1. 抓包
    2. 2.2. 写代码

好久没更新博客的某h回来了

最近发现了好东西 Bing今日美图
每天都有不一样的挺好的图片
非常适合做壁纸或者是网页的背景

从网上找

首先我想到的百度
然后排在前面的代码是这样的:

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');
}
?>

虽然Bing的API没有大改,但是并不能用,估计这个代码比较久远

那就只能自己丰衣足食了


自己写

抓包

打开Fiddler,然后在浏览器里打开Bing
首先映入眼帘的就是背景图
然后就抓到了向这样的几个链接
抓包图
画红框的很有可能就是目标啦!点进去瞧瞧
分析图
果不其然,就是他了!干他!

写代码

直接贴上好了
保存即可使用
自动跳转

1
2
3
4
5
6
7
8
9
10
11
<?php
// 访问Bing的API
$str = file_get_contents('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1');
if($str){ // 判断是是否成功
$data = json_decode($str, true); // 利用内置函数解析
$image = 'https://www.bing.com' . $data['images'][0]['url']; // 拼接链接
} else { //若失败
$image = '你的备用图片地址'; // 使用备用图片
}
header("Location:$image",true,302); //跳转
?>

本文作者 : hank9999
版权声明 :本站所有文章除特别声明外,均采用 BY-NC-SA 4.0 许可协议。转载请注明出处!
本文链接 : https://blog.hank.ltd/get-bing-picturer-by-php/