百度智能云 之图像搜索
一、文档
https://ai.baidu.com/ai-doc/IMAGESEARCH/Ck3bczreq
二、示例代码
获取token
1、入库
<?php /** * 发起http post请求(REST API), 并获取REST请求的结果 * @param string $url * @param string $param * @return - http response body if succeeds, else false. */ function request_post($url = '', $param = '') { if (empty($url) || empty($param)) { return false; } $postUrl = $url; $curlPost = $param; // 初始化curl $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $postUrl); curl_setopt($curl, CURLOPT_HEADER, 0); // 要求结果为字符串且输出到屏幕上 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // post提交方式 curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); // 运行curl $data = curl_exec($curl); curl_close($curl); return $data; } $token = '[调用鉴权接口获取的token]'; $url = 'https://aip.baidubce.com/rest/2.0/realtime_search/same_hq/add?access_token=' . $token; $img = file_get_contents('[本地文件路径]'); $img = base64_encode($img); $bodys = array( 'brief' => "{"name":"小度", "id":"1"}", 'image' => $img, 'tags' => "1,1" ); $res = request_post($url, $bodys); var_dump($res);
2、检索
<?php /** * 发起http post请求(REST API), 并获取REST请求的结果 * @param string $url * @param string $param * @return - http response body if succeeds, else false. */ function request_post($url = '', $param = '') { if (empty($url) || empty($param)) { return false; } $postUrl = $url; $curlPost = $param; // 初始化curl $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $postUrl); curl_setopt($curl, CURLOPT_HEADER, 0); // 要求结果为字符串且输出到屏幕上 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // post提交方式 curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); // 运行curl $data = curl_exec($curl); curl_close($curl); return $data; } $token = '[调用鉴权接口获取的token]'; $url = 'https://aip.baidubce.com/rest/2.0/realtime_search/same_hq/search?access_token=' . $token; $img = file_get_contents('[本地文件路径]'); $img = base64_encode($img); $bodys = array( 'image' => $img ); $res = request_post($url, $bodys); var_dump($res);