comment
function curlPost($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
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
//百度过滤敏感词 获取token
function getBaiduToken(){
$url = 'https://aip.baidubce.com/oauth/2.0/token';
$post_data=[
'grant_type'=>'client_credentials',
'client_id'=>'evtcQOPlk165cOHSPmIiDLCn',
'client_secret'=>'vmnLZAPEGGEGNcGebOkLC5RLuToOakxM'
];
$o = "";
foreach ($post_data as $k => $v) {
$o .= "$k=" . urlencode($v) . "&";
}
$post_data = substr($o, 0, -1);
$res = curlPost($url, $post_data);
//进行把返回结果转成数组
$res = json_decode($res, true);
if (isset($res['error'])) {
exit('API Key或者Secret Key不正确');
}
$accessToken = $res['access_token'];
return $accessToken;
}
//百度过滤敏感词 返回过滤结果
function testText($data){
$bodys = array(
'text' => $data['comment']
);
$token=getBaiduToken();
$url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=".$token;
$res = curlPost($url,$bodys);
$res = json_decode($res, true);
return $res;
}
控制器调用
public function addComment(Request $request){
$param=$request->post('comment');
$arr=['comment'=>$param];
$res=testText($arr);
print_r($res);
}
|