参考地址:
在线活体检测:ai.baidu.com/ai-doc/FACE…
demo:
<?php
namespace appcommonlogic;
use thinkCache;
class Baidu extends Base
{
private $secret = '';//秘钥
private $appkey = '';
private $appid = '';
private $go_url = '';//访问地址
/**
* 刷新token
* @return hink
esponseJson
*/
protected function refreshToken()
{
$param = [
'grant_type' => 'client_credentials',
'client_id' => $this->appkey,
'client_secret' => $this->secret,
];
$url = $this->go_url . '/oauth/2.0/token?' . http_build_query($param);
$result = httpRequest($url, 1);
return $result;
}
/**
* 获取token
* @return bool|mixed
*/
protected function getToken()
{
$token = Cache::get('baidu_token');//读取缓存token 之前获取的
if ($token) {
return $token;
} else {
$res = $this->refreshToken();
if (isset($res['access_token'])) {
Cache::set('baidu_token', $res['access_token'], 864000);//新获取的token 再次保存
return $res['access_token'];
}
return false;
}
}
/**
* 百度 实名认证
* @param $image 身份证图片
* @param $id_card 身份证号
* @param $name 姓名
* @return bool|mixed
*/
public function realname($image, $id_card, $name)
{
$token = $this->getToken();
$param = [
'image' => $image,
'image_type' => 'URL',
'id_card_number' => $id_card,
'name' => $name,
];
$url = $this->go_url . '/rest/2.0/face/v3/person/verify?access_token=' . $token;
$result = $this->request_post($url, json_encode($param));
return $result;
}
/**
* 百度 活体检测
* @param $image 检测的照片
* @return bool|mixed
*/
public function imgcheck($image)
{
$token = $this->getToken();
$param = [[
'image' => $image,
'image_type' => 'URL',
]];
$url = $this->go_url . '/rest/2.0/face/v3/faceverify?access_token=' . $token;
$result = $this->request_post($url, json_encode($param));
return $result;
}
function jcHttpDataPost($url, $data, $json_transfer = 1)
{
if ($json_transfer == 1) {
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
} else if ($json_transfer == 2) {
$data = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$status = curl_exec($ch);
curl_close($ch);
$res = json_decode($status, true);
return $res;
}
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_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// 运行curl
$data = curl_exec($curl);
curl_close($curl);
$res = json_decode($data, true);
return $res;
}
}