封装token
/**
* 获取百度ai token
*/
public function get_baidu_token() {
$api_key = "V5aCBH";
$api_secret = "XIZ1UHk";
$url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" . $api_key . "&client_secret=" . $api_secret . "&";
$ret1 = CURLSend($url, $type = 'get', $arr = '');
$ret = json_decode($ret1, true);
if (isset($ret['access_token'])) {
Cache::set("baidu_ai_access_token", $ret['access_token'], 2592000);
}
// echo "";var_dump( $ret,$ret['refresh_token'] );die;
return $ret['access_token'];
}
/**
* 模拟浏览器发送
*/
function CURLSend($url, $method = 'get', $data = '') {
$ch = curl_init(); //初始化
$headers = array('Accept-Charset: utf-8');
curl_setopt($ch, CURLOPT_URL, $url); //指定请求的URL
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); //提交方式
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //不验证SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //不验证SSL
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置HTTP头字段的数组
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible;MSIE5.01;Windows NT 5.0)'); //头的字符串
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); //自动设置header中的Referer:信息
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //提交数值
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //是否输出到屏幕上,true不直接输出
$temp = curl_exec($ch); //执行并获取结果
curl_close($ch);
return $temp; //return 返回值
}
地址
$accessTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token';//获取token url
$textUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined';//文本审核url
$imgUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined';//图片审核url
$avatarUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/face_audit';//头像审核url
调用文本审核
// 百度审核
$access_token = Cache::get("baidu_ai_access_token");
if (!$access_token) {
$access_token = $this->get_baidu_token();
}
$url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" . $access_token;
$text_arr = [
"text" => "法论功",
];
$baidu_ret = CURLSend( $url, 'post', $text_arr );
$baidu_ret = json_decode($baidu_ret,true);
// 审核结果类型,可取值1.合规,2.不合规,3.疑似,4.审核失败
if( $baidu_ret['conclusionType'] != 1 ){
$this->error( "内容不合法规" );
}
echo "";var_dump( $baidu_ret );die;
调用图片审核
$access_token = Cache::get("baidu_ai_access_token");
if (!$access_token) {
$access_token = $this->get_baidu_token();
}
$url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=" . $access_token;
$imgType = 0; // 图片类型0:静态图片,1:gif动态图片
$text_arr = [
"imgUrl" => "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597403883081&di=182c250e755f81710c3856cd45190887&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180122%2F215670c36d494bbc8b6858ee9b10b175.jpeg",
"imgType" => 0,
];
$baidu_ret = CURLSend( $url, 'post', $text_arr );
$baidu_ret = json_decode($baidu_ret,true);
echo "";var_dump( $baidu_ret );die;
// 审核结果类型,可取值1、2、3、4,分别代表1:合规,2:不合规,3:疑似,4:审核失败
if( $baidu_ret['conclusionType'] != 1 ){
$this->error( "内容不合法规" );
}
调用身份证/护照识别
$access_token = Cache::get("baidu_ai_access_token");
if (!$access_token) {
$access_token = $this->get_baidu_token();
}
if ($v['type'] == 1) {
$url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" . $access_token;
} elseif ($v['type'] == 2) {
$url = "https://aip.baidubce.com/rest/2.0/ocr/v1/passport?access_token=" . $access_token;
}
// $img = file_get_contents( "http://admins.alet.com//uploads/personal_certificate/20200208/compress_989a025c93cabb9204b67efa86a0e08b.jpg" );
$img = file_get_contents($domain_url . "/" . $v["front_image"]);
$img = base64_encode($img);
$arr['image'] = $img;
$arr['id_card_side'] = "front";
$ret1 = controller("Common")->http_curl($url, $type = 'post', $arr);
$ret = json_decode($ret1, true);
// echo "";var_dump( $ret , $access_token , $v );die;
if (isset($ret['error_code']) && $ret['error_code'] > 111) {
Db::name('app_certificate')->where("user_id", $v['user_id'])->update(["status" => 3, "update_time" => time(), "examine_num" => $v['examine_num'] + 1]);
continue;
} elseif (isset($ret['error_code']) && $ret['error_code'] < 112) {
break;
}
if ($v['type'] == 1) {
$name = $ret['words_result']['姓名']['words'];
$num = $ret['words_result']['公民身份号码']['words'];
} elseif ($v['type'] == 2) {
$name = $ret['words_result']['姓名']['words'];
$num = $ret['words_result']['护照号码']['words'];
}
多线程请求
/**
* @param string $url
* @param array $datas HTTP POST BODY
* @param array $param HTTP URL
* @param array $headers HTTP header
* @return array
*/
public function multi_post($urls=array(), $datas=array(), $headers=array()){
$map = [];
$queue = curl_multi_init();
foreach ($urls as $key => $url) {
$data = $datas[$key];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible;MSIE5.01;Windows NT 5.0)');
curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $datas[$key]);
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
curl_multi_add_handle($queue, $ch);
$map[(string) $ch] = $url;
}
$responses = array();
do {
while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM);
if ($code != CURLM_OK) { break; }
while ($done = curl_multi_info_read($queue)) {
$error = curl_error($done['handle']);
$results = curl_multi_getcontent($done['handle']);
// $responses[$map[(string) $done['handle']]] = compact('error', 'results');
$responses[] = compact('error', 'results');
curl_multi_remove_handle($queue, $done['handle']);
curl_close($done['handle']);
}
if ($active > 0) {
curl_multi_select($queue, 0.5);
}
} while ($active);
curl_multi_close($queue);
// echo "";var_dump( $responses );die;
return $responses;
}
多线程请求图片
$access_token = Cache::get("baidu_ai_access_token");
if (!$access_token) {
$access_token = $this->get_baidu_token();
}
$url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=" . $access_token;
$imgType = 0; // 图片类型0:静态图片,1:gif动态图片
$text_arr = [
"imgUrl" => "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597409432877&di=9da64a4cc20eaa2722a3208f11d84b3a&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20161209%2F669124c1392b4d63997c7b1b61b0d3fb_th.jpg",
"imgType" => 0,
];
$urls = [
0 => $url,
1 => $url,
2 => $url,
];
$text_arr1 = [
"imgUrl" => "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597409384556&di=91fa8cac65f535edb9a502119463ff49&imgtype=0&src=http%3A%2F%2Fwww.chinacpc.com.cn%2Fupload%2Ffckfile%2Fimage%2Fimage%2F20150421-8.jpg",
"imgType" => 0,
];
$text_arr2 = [
"imgUrl" => "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597409432880&di=4dbec2215088672c8f5b3f9e26c52d52&imgtype=0&src=http%3A%2F%2Fimg.baizhan.net%2Fuploads%2Fallimg%2F150331%2F20_150331225304_1.jpg",
"imgType" => 0,
];
$datas = [
0 => $text_arr,
1 => $text_arr1,
2 => $text_arr2,
];
$headers = array('Accept-Charset: utf-8');
$baidu_ret = $this->multi_post( $urls, $datas, $headers );
foreach( $baidu_ret as $k => $v ){
// echo "";var_dump( $v );die;
$v['results'] = json_decode($v['results'],true);
if( $v['results']['conclusionType'] != 1 ){
$this->error( "图片不合法规" );
}
}