最近模板兔在给客户用thinkphp对接文本审核功能,因为客户的网站是AIGC模式,内容审核是重点。话不多说,直接附上代码供参考:
function erphp_bd_tms($content){ $error = 1;$msg = ''; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://aip.baidubce.com/oauth/2.0/token?client_id=".ERPHP_BDTMS_APPKEY."&client_secret=".ERPHP_BDTMS_APPSECRET."&grant_type=client_credentials", CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', 'Accept: application/json' ), )); $response = curl_exec($curl); curl_close($curl); $re = json_decode($response,true); if(isset($re['error'])){ $msg = $re['error_description']; }else{ if(isset($re['access_token'])){ $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=' . $re['access_token']; $param = array( 'text' => $content ); $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); $re2 = json_decode($data,true); if(isset($re2['error_code'])){ $msg = $re2['error_msg']; }else{ if(isset($re2['conclusionType']) && $re2['conclusionType'] == '1'){ $error = 0; }else{ $msg = '内容不合规'; } } }else{ $msg = '鉴权出错,请稍后重试'; } } return json( [ "error" => $error, "msg" => $msg ] ); }
函数返回一个json,如果error为0说明审核通过。
0 个评论