大家好,又见面了,我是你们的朋友全栈君。
二话不说 直接上代码
//传入审核文本
public static function test($text)
{
$token = self::getAccessToken();
$url = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=' . $token;
$bodys = array(
'text' =>$text
);
$res =self:: request_post($url, $bodys);
$res = json_decode($res, true);
return $res['conclusion'];
}
//获取token
public static function getAccessToken()
{
$url = 'https://aip.baidubce.com/oauth/2.0/token';
$post_data['grant_type'] = 'client_credentials';
$post_data['client_id'] = 'api-key';
$post_data['client_secret'] = 'secret-key';
$o = "";
foreach ($post_data as $k => $v) {
$o .= "$k=" . urlencode($v) . "&";
}
$post_data = substr($o, 0, -1);
$res = self:: request_post($url, $post_data);
$res = json_decode($res, true);
return $res['access_token'];
}
//封装的发请求
public static 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);
$data = curl_exec($curl);//运行curl
curl_close($curl);
return $data;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/133595.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...