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
并上传到服务器
请求方式: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)"
}
}
错误码说明:
本文共2058个字符,其中有 96 个汉字,平均阅读时长 ≈ 7分钟
评论