效果大家看我现在博客的背景,就是百度的随机图片。
这里要使用到百度的一个接口。

打开浏览器按住F12打开:http://image.baidu.com/channel/wallpaper 随便点击个分类,会发现他会请求一个接口,

请输入图片描述

里面会有需要的json数据

请输入图片描述

那就简单了请求解析就能用,下面是复制出来的接口
http://image.baidu.com/data/imgs?pn=0&rn=36&col=%E5%A3%81%E7%BA%B8&tag=%E9%A3%8E%E6%99%AF&tag3=%E8%87%AA%E7%84%B6%E9%A3%8E%E5%85%89&width=1920&height=1080&ic=0&ie=utf8&oe=utf-8&image_id=&fr=channel&p=channel&from=1&app=img.browse.channel.wallpaper&t=0.1993955611514664


经过我测试得出ic这个好像是页数。tag是大分类,而tag3是小分类,这几个分类改变结果也会跟着改变,OK那就开始写代码吧!HH

/**
 * 调用百度壁纸接口随机生成图片
 * @author Reaper <369994633@qq.com> 
 * @param  string        $Classify  分类
 * @param  string        $Subclass  子类
 * @param  string        $Rand      最大随机页
 * @return string
 */
function BaiduRandImg($Classify,$Subclass,$Rand)
{
    $rand = rand(0,$Rand);
    $Classify = urlencode($Classify);
    $Subclass = urlencode($Subclass);
    $BaiduApiUrl = "http://image.baidu.com/data/imgs?pn=0&rn=36&col=%E5%A3%81%E7%BA%B8&tag=".$Classify."&tag3=".$Subclass."&width=1920&height=1080&ic=".$Rand."&ie=utf8&oe=utf-8&image_id=&fr=channel&p=channel&from=1&app=img.browse.channel.wallpaper&t=0.1993955611514664"; 
    $ImgB = file_get_contents($BaiduApiUrl);
    $JsonGo = json_decode($ImgB,1);
    $Num = count($JsonGo['imgs']);
    $Arrnum = rand(0,$Num-2);
    return $JsonGo['imgs'][$Arrnum]['imageUrl'];
}

//使用跳板来破解防盗链,然后拼接跳转
$GangWay = "https://www.myapi.wang/api/imgapi.php?imgurl=";
$GangWay .= BaiduRandImg("风景","自然风光","10");
header("Location:".$GangWay);

代码中$Arrnum中的Num-2是因为数组是从0开始并且他这里面最后一条数据是空的
因为百度有防盗链所以我加了跳板。
跳板源码大家可以访问下面的文章获取到:

效果地址:https://www.myapi.wang/api/BaiduImg.api.php
不想搭建的可以直接使用我这个调用,我这调用的是风景。

原创文章 未经许可 禁止搬运!

最后修改:2023 年 07 月 25 日
您的赞赏是对我最大的支持。