首先 获取 accesstoken
function request_post($url = '', $param = '') {
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$curl = curl_init();//初始化curl
curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($curl);//运行curl
curl_close($curl);
return $data;
}
function token(){
//获取accesstoken
if(is_file(TOKENFILE)){
$file = file_get_contents(TOKENFILE);
$file = json_decode($file,true);
if(time()-$file['guoqitime']>$file['expires_in']-200){
return newAccessToken();
}else{
return $file['access_token'];
}
}else{
newAccessToken();
}
}
function newAccessToken(){
$url = 'https://aip.baidubce.com/oauth/2.0/token';
$post_data['grant_type'] = 'client_credentials';
$post_data['client_id'] = "QKhi7sXhTjwF7XR8dIYPhR7u";
$post_data['client_secret'] = "rDprrjCcGz1E0OQQYDT1oqT1UjfmpCYs";
$o = "";
foreach ( $post_data as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1);
$res = request_post($url, $post_data);
$res = json_decode($res,true);
$res['guoqitime'] = time();//创建时间
$res = json_encode($res);
file_put_contents(TOKENFILE,$res);
}
获取到以后 构建如下的一组数据
$bodys = array(
"image"=>$find['imgbase'],
"max_face_num"=>5,
"face_fields"=>""
);
其中的 image 变量 需要进行base64位编码 max 是 识别最大的人脸数量 face 是得到那些数据
然后 在调用 百度云 人脸检测接口
https://aip.baidubce.com/rest/2.0/face/v2/detect?access_token=$token
通过 curl 访问 提交数据
成功返回数据示例见百度云接口返回数据