您正在使用 IPV4 [3.87.133.69] 访问本站,您本次已经查看了 1 页
用户名: 密 码: 验证码:     用QQ登录本站
首页 软件 编程 笑话 知识 公告 台风 日历 计算器
[公益]保护绿色环境,构建和谐社会       悟空收录网      

【腾讯云】 爆款2核2G3M云服务器首年 61元,叠加红包再享折上折      
[公益] 地球是我家,绿化靠大家      
2024年 清明节 007
2024年 劳动节 034
2025年 元 旦 279
2025年 春 节 307
 
您现在的位置:首页 >> PHP >> 内容
本类新增
本类热门
PHP利用百度ai实现文本和图片审核
内容摘要: 首先打开百度ai开发平台注册一个账号:注册账号,进入控制台创建自己的应用,获取apikey和秘钥进入文档页文本审核:图像审核:文档很详细,实现用户发布内容审核图片审核还是很方便简单的。classSentive{protected$accessTokenUrl='https://aip.baidubce.com/oauth/2.0/token';//获取tok......
首先打开百度ai开发平台注册一个账号:

注册账号,进入控制台

创建自己的应用,获取apikey和秘钥

进入文档页文本审核:

图像审核:

文档很详细,实现用户发布内容审核图片审核还是很方便简单的。

classSentive

{

protected$accessTokenUrl='https://aip.baidubce.com/oauth/2.0/token';//获取tokenurl

protected$textUrl='https://aip.baidubce.com/rest/2.0/antispam/v2/spam';//文本审核url

protected$imgUrl='https://aip.baidubce.com/api/v1/solution/direct/img_censor';//图片审核url

protected$avatarUrl='https://aip.baidubce.com/rest/2.0/solution/v1/face_audit';//头像审核url

protected$grant_type;

protected$client_id;

protected$client_secret;

function__construct()

{

$this->grant_type='client_credentials';

$this->client_id='xxx';//APIKey

$this->client_secret='xxx';//SecretKey

}

staticfunctionrequest($url='',$param='')

{

if(empty($url)||empty($param)){

returnfalse;

}

$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;

}

staticfunctionrequest_post($url='',$param=array(),$type)

{

if(empty($url)||empty($param)){

returnfalse;

}

$postUrl=$url;

$curlPost=$param;

$curl=curl_init();

curl_setopt($curl,CURLOPT_URL,$postUrl);

curl_setopt($curl,CURLOPT_HEADER,0);

//要求结果为字符串

curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);

//post方式

curl_setopt($curl,CURLOPT_POST,1);

curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);

curl_setopt($curl,CURLOPT_POSTFIELDS,$curlPost);

if($type=="text"){

curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type:application/x-www-form-urlencoded'));

}else{

curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type:application/json;charset=utf-8'));

}

curl_setopt($curl,CURLINFO_HEADER_OUT,true);

$data=curl_exec($curl);

$code=curl_getinfo($curl,CURLINFO_HTTP_CODE);

if($code===0){

thrownew\Exception(curl_error($curl));

}

curl_close($curl);

return$data;

}

//获取token

publicfunctiongetToken()

{

newRedis();

$post_data['grant_type']=$this->grant_type;

$post_data['client_id']=$this->client_id;

$post_data['client_secret']=$this->client_secret;

$o="";

foreach($post_dataas$k=>$v){

$o.="$k=".urlencode($v)."&";

}

$post_data=substr($o,0,-1);

$res=self::request($this->accessTokenUrl,$post_data);

$redis->setkey("filterToken",json_decode($res,true)['access_token']);

returnjson_decode($res,true)['access_token'];

}

//文本审核

publicfunctiontextVerify($data)

{

newRedis();

$token=$redis->get("filterToken");

if(empty($token)){

$token=$this->getToken();

}

$curl=$this->textUrl."?access_token=".$token;

$result=self::request_post($curl,$data,"text");

returnjson_decode($result,true);

}

//图片审核

publicfunctionimgVerify($img)

{

$redis=newRedis();

$token=$redis->get("filterToken");

if(empty($token)){

$token=$this->getToken();

}

$curl=$this->imgUrl."?access_token=".$token;

$bodys=array(

'image'=>$img,

'scenes'=>array("ocr",

"face","public","politician","antiporn","terror","webimage","disgust",

'watermark')

);

$bodys=json_encode($bodys);

$result=self::request_post($curl,$bodys,"img");

returnjson_decode($result,true);

}

//头像审核

publicfunctionavatarVerify($img)

{

$redis=newRedis();

$token=$redis->get("filterToken");

if(empty($token)){

$token=$this->getToken();

}

$curl=$this->avatarUrl."?access_token=".$token;

$bodys=array(

"configId"=>"1",

"images"=>$img

);

$result=self::request_post($curl,$bodys,"text");

returnjson_decode($result,true);

}

}

版权声明:本内容来源于网络,如有侵犯您的版权,请联系站长,本站收到您的信息后将及时处理。
上一篇:php判断是否连接上网络的方法实例详解

 

下一篇:PHP随机字符串生成代码(包括大小写字母)

发布日期:2023/5/19
手机扫二维码直达本页
发布时间:12:36:12
点  击:2
录  入:齐天大圣
相关文章
Baidu
YiJiaCMS 7.3.8 build231228(MSSQL) 闽ICP备05000814号-1
本空间由腾讯云(轻量应用服务器)提供,Cloudflare提供加速防护
运行时间载入中.....