百度文本审核api_利用百度ai实现文本和图片审核

本文介绍了如何利用百度AI的文本审核和图像审核API来替代自建的违禁词库系统。通过注册百度AI开发平台账号,获取API密钥,并结合PHP代码示例,实现了内容发布的便捷审核功能。
摘要由CSDN通过智能技术生成

之前做平台内容发布审核都是自己构建一套违禁词库,在代码中利用词库判断用户发布的内容,现在可以使用百度ai api完成这个功能。接下来就简单说下怎么做吧:

首先打开百度ai 开发平台 注册一个账号

c779b03ba4480d4e51f3f02e3638b85d.png
c46c36b821ede96d4364b72aebd4d324.png

进入控制台

a865e3ac82a5855d4576abcfdc3b5141.png

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

ad7ee8617df03ca46bc789b45661b0a4.png

进入文档页 文本审核:

8fb196ed7692c2f967d3fcc47188c5d0.png

图像审核:

7d7307ad436fbb75a0063b727d4653d2.png

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

我没有使用官方的sdk,简单的整合了一下作为练手,以下是我简单使用php实现的代码demo:

use NntControllerApplication;class Sentive{ protected $accessTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token';//获取token url 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';//API Key $this->client_secret = 'xxx';//Secret Key } static function request($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; } static function request_post($url = '', $param = array(), $type){ if (empty($url) || empty($param)) { return false; } $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) { throw new Exception(curl_error($curl)); } curl_close($curl); return $data; } //获取token public function getToken(){ $redis = Application::$shared->di->getRedis(); $post_data['grant_type'] = $this->grant_type; $post_data['client_id'] = $this->client_id; $post_data['client_secret'] = $this->client_secret; $o = ""; foreach ($post_data as $k => $v) { $o .= "$k=" . urlencode($v) . "&"; } $post_data = substr($o, 0, -1); $res = self::request($this->accessTokenUrl, $post_data); $redis->setkey("filterToken
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值