获取bing每日图片API
侧边栏壁纸
  • 累计撰写 56 篇文章
  • 累计收到 0 条评论
  • 本站共 9.27 W

获取bing每日图片API

陌路离殇
2024-10-26 / 0 评论 / 10 阅读 / 正在检测是否收录...

PHP代码

<?php
header('Content-type: application/json;charset=utf-8');
header('Access-Control-Allow-Origin:*');

$str=http_curl_get('http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1','cn.bing.com','http://cn.bing.com/');
if(preg_match_all("/<url>(.+?)<\/url>/",$str,$matches)){
    $url='https://s.cn.bing.net'.$matches[1][0];
}
if(preg_match_all("/<copyright>(.+?)<\/copyright>/",$str,$matches)){
    $location=$matches[1][0];
}
if($url == NULL){
    $Json = [
        "code"=> 201,
        "msg" => "获取信息失败,请重试",
        "time"=>date('Y-m-d H:i:s',time()),
        "data"=>[]
    ];
}else{
    $Json = [
        "code"=> 200,
        "msg"=>"success",
        "time"=>date('Y-m-d H:i:s',time()),
        "data"=>[
            "url"=>$url,
            "location"=>$location,
        ]
    ];
}
$Json = json_encode($Json,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
echo stripslashes($Json);

function http_curl_get($url,$hosts,$referer){
    $cip = mt_rand(11, 191) . "." . mt_rand(0, 240) . "." . mt_rand(1, 240) . "." . mt_rand(1, 240);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_ENCODING, '');
    curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Host:'.$hosts,'X-FORWARDED-FOR:'.$cip]);
    curl_setopt($curl,CURLOPT_REFERER,$referer);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 50);
    if(curl_errno($curl)){
        $data = 'Error:' . curl_error($curl);
    }else{
        $data = curl_exec($curl);
    }
    return $data;
}
?>


使用方法

将代码保存到 bing.php 并上传到服务器
请求示例:http://localhost/bing.php
请求方式:GET/POST
返回格式:JSON
返回参数说明:
名称说明
code状态码
msg返回提示信息
time请求时间
url返回图片地址
location返回风景地址
返回示例:
{
    "code": 200,
    "msg": "success",
    "time": "2022-11-10 22:05:20",
    "data": {
        "url": "https://s.cn.bing.net/th?id=OHR.BadLightning_ROW1865809950_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
        "location": "Rock formations, Badlands National Park, South Dakota, USA (© DEEPOL by plainpicture)"
    }
}
错误码说明:
名称说明
201未获取到图片信息

本文共2058个字符,其中有 96 个汉字,平均阅读时长 ≈ 7分钟
0

打赏

海报

正在生成.....

评论

博主关闭了所有页面的评论