PHP利用百度ai实现文本和图片审核
网络编程 2021-07-04 23:24www.168986.cn编程入门
这篇文章主要介绍了PHP利用百度ai实现文本和图片审核,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧
之前做平台内容发布审核都是自己构建一套违禁词库,在代码中利用词库判断用户发布的内容,现在可以使用百度ai api完成这个功能。接下来就简单说下怎么做吧
打开百度ai 开发平台 注册一个账号
注册账号,进入控制台
创建自己的应用,获取apikey 和秘钥
进入文档页 文本审核
图像审核
文档很详细,实现用户发布内容审核 图片审核还是很方便简单的。我没有使用官方的sdk,简单的整合了一下作为练手,以下是我简单的代码demo:
class Sentive { protected $aessTokenUrl = 'https://aip.baidubce./oauth/2.0/token';//获取token url protected $textUrl = 'https://aip.baidubce./rest/2.0/antispam/v2/spam';//文本审核url protected $imgUrl = 'https://aip.baidubce./api/v1/solution/direct/img_censor';//图片审核url protected $avatarUrl = 'https://aip.baidubce./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_set($curl, CURLOPT_URL, $postUrl);//抓取指定网页 curl_set($curl, CURLOPT_HEADER, 0);//设置header curl_set($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_set($curl, CURLOPT_POST, 1);//post提交方式 curl_set($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_set($curl, CURLOPT_URL, $postUrl); curl_set($curl, CURLOPT_HEADER, 0); // 要求结果为字符串 curl_set($curl, CURLOPT_RETURNTRANSFER, 1); // post方式 curl_set($curl, CURLOPT_POST, 1); curl_set($curl, CURLOPT_SSL_VERIFYPEER, false); curl_set($curl, CURLOPT_POSTFIELDS, $curlPost); if ($type == "text") { curl_set($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x--form-urlencoded')); } else { curl_set($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8')); } curl_set($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() { new Redis(); $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->aessTokenUrl, $post_data); $redis->setkey("filterToken", json_decode($res, true)['aess_token']); return json_decode($res, true)['aess_token']; } //文本审核 public function textVerify($data) { new Redis(); $token = $redis->get("filterToken"); if (empty($token)) { $token = $this->getToken(); } $curl = $this->textUrl . "?aess_token=" . $token; $result = self::request_post($curl, $data, "text"); return json_decode($result, true); } //图片审核 public function imgVerify($img) { $redis = new Redis(); $token = $redis->get("filterToken"); if (empty($token)) { $token = $this->getToken(); } $curl = $this->imgUrl . "?aess_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"); return json_decode($result, true); } //头像审核 public function avatarVerify($img) { $redis = new Redis(); $token = $redis->get("filterToken"); if (empty($token)) { $token = $this->getToken(); } $curl = $this->avatarUrl . "?aess_token=" . $token; $bodys = array( "configId" => "1", "images" => $img ); $result = self::request_post($curl, $bodys, "text"); return json_decode($result, true); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
上一篇:php查询内存信息操作示例
下一篇:PHP回调函数简单用法示例
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程