今天给大家带来一个通过php代码爬取bing每日图片的程序,很简单,代码如下:
<?php
$url = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if ($data && isset($data['images'][0])) {
$imageUrlBase = "https://cn.bing.com"; // 图片的基础URL部分
$imageInfo = $data['images'][0];
$imageUrl = $imageUrlBase . $imageInfo['url']; // 完整的图片URL
header('Location:'.$imageUrl);
}
?>
或者用下面的更简单:
<?php
$json = file_get_contents("https://cn.bing.com/HPImageArchive.aspx?format=js&n=1"); //获取JSON
$data = json_decode($json, true); //解码获取到的JSON
$imageUrl = 'https://cn.bing.com'.$data['images'][0]['urlbase'].'_UHD.jpg'; //如需更低清晰度,将 '_UHD.jpg' 换成 '_1080x1920.jpg' 即可
header('Location:'.$imageUrl);
?>
怎么样是不是很简单?喜欢你就试试吧
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)