ThinkPHP60 实现 图片审核+文本内容审核(敏感词

应用场景

  • 用户评论过滤:对网站用户的评论信息进行检测,审核出涉及违规内容,保证良好的用户体验
  • 注册信息筛查:对用户的注册信息进行筛查,避免黑产通过用户名实现违规信息的推广
  • 文章内容审核:对UGC文章内容进行多个维度的审核,避免因内容违规导致的APP下架等损失

开通应用

1.内容审核控制台:
console.bce.baidu.com/ai/?fromai=…

image.png

2.领取免费资源

image.png

image.png

3.创建应用

image.png

image.png

  1. 查看应用信息

image.png

这里面有我们需要的配置信息

实战

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
php复制代码<?php
/**
* Author: 柯作
* Email: kezuo@foxmail.com
* Date: 2021/9/2
* Time: 11:15
*/

namespace app\api\controller;


use app\Request;

class Audit
{

/**
* 内容审核
*/
public function contentAudit(Request $request)
{
$content = $request->post('content');
$token = $this->getAccessToken('API Key', 'Secret Key');

$url = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=' . $token;
$bodys = array(
'text' => $content
);
$res = $this->curlPost($url, $bodys);

//结果转成数组
$res = json_decode($res, true);
//根据自己的业务逻辑进行处理
print_r($res);die;
}

/**
* 图片审核
*/
public function imageAudit()
{

$token = $this->getAccessToken('API Key', 'Secret Key');
$url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=' . $token;
$img = file_get_contents('C:\Users\Pictures\Saved Pictures\1.png');
$img = base64_encode($img);
$bodys = array(
'image' => $img
);
$res = $this->curlPost($url, $bodys);
//结果转成数组
$res = json_encode($res, true);
//根据自己的业务逻辑进行处理
print_r($res);
}

/**
* CURL的Post请求方法
* @param string $url
* @param string $param
* @return bool|string
*/
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;
}

/**
* 获取百度开放平台的票据
* 参考链接:https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu
*/
public function getAccessToken($ApiKey = '', $SecretKey = '', $grantType = 'client_credentials')
{

$url = 'https://aip.baidubce.com/oauth/2.0/token';
$post_data['grant_type'] = $grantType;
$post_data['client_id'] = $ApiKey;
$post_data['client_secret'] = $SecretKey;
$o = "";
foreach ($post_data as $k => $v) {
$o .= "$k=" . urlencode($v) . "&";
}
$post_data = substr($o, 0, -1);

$res = $this->curlPost($url, $post_data);
//进行把返回结果转成数组
$res = json_decode($res, true);
if (isset($res['error'])) {
exit('API Key或者Secret Key不正确');
}
$accessToken = $res['access_token'];
return $accessToken;
}
}

配置路由,进行调用就行

image.png

文本内容审核测试

1.输入文本为‘你好’

image.png

结果为合规

2.输入文本内容为‘敏感词’

image.png

结果则为不合规

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%