阿里云敏感词安全接口PHP实现

本文最后更新于:3 年前

不啰嗦直接上代码

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
class AliyunGreen
{
private $accessKeyId;
private $accessKeySecret;
private $url = 'https://green.%s.aliyuncs.com%s';
private $api;
private $signatureNonce = '';

public function __construct($accessKeyId, $accessKeySecret, $area, $api, $signatureNonce)
{
$this->accessKeyId = $accessKeyId;
$this->accessKeySecret = $accessKeySecret;
$this->api = $api;
$this->url = \sprintf($this->url, $area, $api);
$this->signatureNonce = $signatureNonce;
}

/**
* @param string $body
* @param string $clientInfo
* @return mixed
*/
private function setQueryUrl(string $body, string $clientInfo)
{
$headers = $this->setHeader($body);

$str = "POST" . PHP_EOL;
foreach ($headers as $k => $v) {
if (strpos($k, 'x-acs') === 0) {
$str .= $k . ':' . $v . PHP_EOL;
} else {
$str .= $v . PHP_EOL;
}
}
if (isset($clientInfo) && $clientInfo != '') {
$str .= "$this->api?clientInfo=" . $clientInfo;
} else {
$str .= $this->api;
}

$signature = base64_encode(hash_hmac("sha1", $str, $this->accessKeySecret, true));
$Authorization = ['Authorization' => "acs $this->accessKeyId:$signature"];

$httpHeader = array_merge($headers, $Authorization);
$queryHeader = [];
foreach ($httpHeader as $k => $v) {
$queryHeader[] = $k . ':' . $v;
}
if (isset($clientInfo) && $clientInfo != '') {
$this->url = $this->url . '?clientInfo=' . urlencode($clientInfo);
}

$res = $this->curl($this->url, true, $body, $queryHeader);
return json_decode($res, 1);
}

/**
* @param string $body
* @return array
*/
private function setHeader(string $body): array
{
$headers = [
'Accept' => 'application/json',
'Content-MD5' => base64_encode(md5($body, 1)),
'Content-Type' => 'application/json',
'Date' => gmdate('D, d M Y H:i:s T', time()), //'Tue, 17 Jan 2017 10:16:36 GMT',注意时区
'x-acs-version' => '2018-05-09',
'x-acs-signature-nonce' => $this->signatureNonce == '' ? $this->getSignatureNonce(10) : $this->signatureNonce,
'x-acs-signature-version' => '1.0',
'x-acs-signature-method' => 'HMAC-SHA1',
];
ksort($headers);
return $headers;
}

/**
* @param string $url
* @param bool $isPost
* @param $arr
* @param array $header
* @return bool|string
*/
public function curl(string $url, bool $isPost, $arr, array $header)
{
try {
$ch = curl_init();
if ($ch === false) {
throw new Exception('failed to initialize');
}
if (stripos($url, "https://") !== false) {
//关闭证书
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 0); //返回response头部信息
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//post方式
if (!empty($isPost)) {
if (is_array($arr)) {
$content = http_build_query($arr); //入参内容
} else {
$content = $arr;
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content); //所传参
}

$sContent = curl_exec($ch);
if ($sContent === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
$aStatus = curl_getinfo($ch);
curl_close($ch);
return $sContent;
} catch (Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
}

/**
* @param int $length
* @return string
*/
private function getSignatureNonce(int $length): string
{
$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$len = strlen($str) - 1;
$randstr = '';
for ($i = 0; $i < $length; $i++) {
$num = mt_rand(0, $len);
$randstr .= $str[$num];
}
return $randstr;
}

// 文本审核
public function textScan($content)
{
$tasks = $this->setQueryUrl($content, '');
print_r($tasks);
}

// 图片审核
public function imageScan($content)
{
$tasks = $this->setQueryUrl($content, '');
print_r($tasks);
}
}

使用方法

1
2
3
4
5
6
7
8
9
10
// 使用方式
$text = new AliyunGreen('id', 'secret', 'cn-beijing', '/green/text/scan',
'');
$body = '{"scenes": ["antispam"],"tasks": [{"dataId": "cfd33235-71a4-468b-8137-a5ffe323a7e8","content": "本校小额贷款,安全、快捷、方便、无抵押,随机随贷,当天放款,上门服务。"}]}';
print_r($text->textScan($body));

$image = new AliyunGreen('id', 'secret', 'cn-shenzhen', '/green/image/scan',
'');
$body = '{"scenes":["porn"],"tasks":[{"dataId":"test2NInmO$tAON6qYUrtCRgLo-1mwxdi","url":"https://img.alicdn.com/tfs/TB1urBOQFXXXXbMXFXXXXXXXXXX-1442-257.png"}]}';
print_r($image->imageScan($body));

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!