百度搜图API源码(历史恢复文章)

发布于 2023-03-10  26 次阅读


该文章恢复至 2020年09月24日 的文章

代码如下:

<?php
if($_GET['txt']==null){
    echo '请输入参数!';
}elseif($_GET['img']=='img'){
$html=file_get_contents("https://m.baidu.com/sf/vsearch/image/search/wisesearchresult?ie=utf-8&word=".$_GET['txt']);
    $p = '/hoverUrl":"(.*?)","dataId":/';
    $s = preg_match_all($p,$html,$t);
    $img = str_ireplace('\\','',$t[1][rand(0,$s)]);
    $im = imagecreatefromjpeg($img);
    header('Content-Type: image/jpeg');
    imagejpeg($im);
    imagedestroy($im);
}else{
    $html=file_get_contents("https://m.baidu.com/sf/vsearch/image/search/wisesearchresult?ie=utf-8&word=".$_GET['txt']);
    $p = '/hoverUrl":"(.*?)","dataId":/';
    $s = preg_match_all($p,$html,$t);
    echo str_ireplace('\\','',$t[1][rand(0,$s)]);
}

源接口来自百度搜图,国外的服务器搭建搜图API效果会比较慢,推荐使用国内的服务器。

调用方法:

http://域名/api.php?txt=需要搜索的内容

直接返回图片链接。

http://域名/api.php?txt=需要搜索的内容&img=img

直接返回图片。

例子:

https://api.cv0.cn/api.php?id=4&txt=御坂美琴

测试接口源自 初春API

最后更新于 2023-03-10