如何用PHP实现拍照找图
此功能类似淘宝拍照找图,以图找图功能 (需要对接百度接口)
相似图搜索文档地址: https://ai.baidu.com/ai-doc/IMAGESEARCH/3k3bczqz8
下面是个简单的例子,只是简单的请求一个,如有需要例子或者demo请留言我,或者加入QQ: 846951943
<?php $api_key = '你的api_key'; $secret_key = '你的secret_key'; //首先先获取access_token ,因为请求身份证验证接口需要用到 ,请求access_token 有效期是30天 我这里没有保存, 如需要你们可以保存到session中 //获取access_token 返回参数请参考 https://ai.baidu.com/ai-doc/REFERENCE/Ck3djhhu $access_token = getAccessToken($api_key,$secret_key); $url = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/add?access_token=' . $access_token; //入库 //$url = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/search?access_token=' . $access_token; //检索 //$url = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/delete?access_token=' . $access_token; //删除 //$url = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/update?access_token=' . $access_token; //更新 $base64 = $_POST['base64']; //在这个地方处理base64 图片 $brief = [ //存到百度的唯一标识 'goods_id' => 1 ]; $res = request_post($url,['brief' => json_encode($brief), 'image' => $base64,]); /** * 创建一个curl请求,用来请求第三方 * @param $url * @param array $data * @return array|bool|float|int|mixed|stdClass|string|null * User: wang * Date: 2021/9/6 18:13 */ function request_post($url , $data=array()){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $output = curl_exec($ch); curl_close($ch); return json_decode($output,true); } /** * 获取AccessToken * @param $api_key * @param $secret_key * @return mixed|string * User: wang * Date: 2021/9/6 18:30 */ function getAccessToken($api_key,$secret_key){ $url = 'https://aip.baidubce.com/oauth/2.0/token'; $post_data['grant_type'] = 'client_credentials'; $post_data['client_id'] = $api_key; $post_data['client_secret'] = $secret_key; $o = ""; foreach ( $post_data as $k => $v ) { $o.= "$k=" . urlencode( $v ). "&" ; } $post_data = substr($o,0,-1); $res = request_post($url, $post_data); if(!isset($res['access_token'])){ exit($res['error']); } return $res['access_token']; }