步骤:

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

注册账号,进入控制台

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

进入文档页 文本审核:

图像审核:

代码实例:

  1 class Sentive
  2 {
  3
  4   protected $accessTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token';//获取token url
  5
  6   protected $textUrl = 'https://aip.baidubce.com/rest/2.0/antispam/v2/spam';//文本审核url
  7
  8   protected $imgUrl = 'https://aip.baidubce.com/api/v1/solution/direct/img_censor';//图片审核url
  9
 10   protected $avatarUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/face_audit';//头像审核url
 11
 12   protected $grant_type;
 13
 14   protected $client_id;
 15
 16   protected $client_secret;
 17
 18   function __construct()
 19 {
 20
 21     $this->grant_type = 'client_credentials';
 22
 23     $this->client_id = 'xxx';//API Key
 24
 25     $this->client_secret = 'xxx';//Secret Key
 26
 27   }
 28
 29   static function request($url = '', $param = '')
 30 {
 31
 32     if (empty($url) || empty($param)) {
 33
 34       return false;
 35
 36     }
 37
 38     $postUrl = $url;
 39
 40     $curlPost = $param;
 41
 42     $curl = curl_init();//初始化curl
 43
 44     curl_setopt($curl, CURLOPT_URL, $postUrl);//抓取指定网页
 45
 46     curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
 47
 48     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
 49
 50     curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
 51
 52     curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
 53
 54     $data = curl_exec($curl);//运行curl
 55
 56     curl_close($curl);
 57
 58     return $data;
 59
 60   }
 61
 62   static function request_post($url = '', $param = array(), $type)
 63 {
 64
 65     if (empty($url) || empty($param)) {
 66
 67       return false;
 68
 69     }
 70
 71     $postUrl = $url;
 72
 73     $curlPost = $param;
 74
 75     $curl = curl_init();
 76
 77     curl_setopt($curl, CURLOPT_URL, $postUrl);
 78
 79     curl_setopt($curl, CURLOPT_HEADER, 0);
 80
 81     // 要求结果为字符串
 82
 83     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 84
 85     // post方式
 86
 87     curl_setopt($curl, CURLOPT_POST, 1);
 88
 89     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 90
 91     curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
 92
 93     if ($type == "text") {
 94
 95       curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
 96
 97     } else {
 98
 99       curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
100
101     }
102
103     curl_setopt($curl, CURLINFO_HEADER_OUT, true);
104
105     $data = curl_exec($curl);
106
107     $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
108
109     if ($code === 0) {
110
111       throw new \Exception(curl_error($curl));
112
113     }
114
115     curl_close($curl);
116
117     return $data;
118
119   }
120
121   //获取token
122
123   public function getToken()
124 {
125
126     new Redis();
127
128     $post_data['grant_type'] = $this->grant_type;
129
130     $post_data['client_id'] = $this->client_id;
131
132     $post_data['client_secret'] = $this->client_secret;
133
134     $o = "";
135
136     foreach ($post_data as $k => $v) {
137
138       $o .= "$k=" . urlencode($v) . "&";
139
140     }
141
142     $post_data = substr($o, 0, -1);
143
144     $res = self::request($this->accessTokenUrl, $post_data);
145
146     $redis->setkey("filterToken", json_decode($res, true)['access_token']);
147
148     return json_decode($res, true)['access_token'];
149
150   }
151
152   //文本审核
153
154   public function textVerify($data)
155 {
156
157     new Redis();
158
159     $token = $redis->get("filterToken");
160
161     if (empty($token)) {
162
163       $token = $this->getToken();
164
165     }
166
167     $curl = $this->textUrl . "?access_token=" . $token;
168
169     $result = self::request_post($curl, $data, "text");
170
171     return json_decode($result, true);
172
173   }
174
175   //图片审核
176
177   public function imgVerify($img)
178 {
179
180     $redis = new Redis();
181
182     $token = $redis->get("filterToken");
183
184     if (empty($token)) {
185
186       $token = $this->getToken();
187
188     }
189
190     $curl = $this->imgUrl . "?access_token=" . $token;
191
192     $bodys = array(
193
194       'image' => $img,
195
196       'scenes' => array("ocr",
197
198         "face", "public", "politician", "antiporn", "terror", "webimage", "disgust",
199
200         'watermark')
201
202     );
203
204     $bodys = json_encode($bodys);
205
206     $result = self::request_post($curl, $bodys, "img");
207
208     return json_decode($result, true);
209
210   }
211
212   //头像审核
213
214   public function avatarVerify($img)
215 {
216
217     $redis = new Redis();
218
219     $token = $redis->get("filterToken");
220
221     if (empty($token)) {
222
223       $token = $this->getToken();
224
225     }
226
227     $curl = $this->avatarUrl . "?access_token=" . $token;
228
229     $bodys = array(
230
231       "configId" => "1",
232
233       "images" => $img
234
235     );
236
237     $result = self::request_post($curl, $bodys, "text");
238
239     return json_decode($result, true);
240
241   }
242
243 }

链接:https://mp.weixin.qq.com/s/X10ZadKVTyWcx3xM4RN3Ww

01-05 09:37