| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace common\components\sensitivewords;
- use addons\Material\common\enums\MaterialEnum;
- use common\components\sensitivewords\BaiduCache;
- use common\enums\SettingEnum;
- use common\models\common\MallSetting;
- /**
- * Class Common
- * @package app\plugins\material\forms\common
- */
- class SensitiveWords
- {
- public $config;
- public static $instance;
- const BAIDUTAXT = 'baidutaxt';
- /**
- * @param $text
- * @return array
- */
- public function getTextStatus($text,$mall_id){
- if(empty($text)){
- return ['status' => 0,'msg' => '文本不许为空'];
- }
- $config = MallSetting::getConfByGroup($mall_id, 'sensitive', true);
- $token = $this->getToken($config['api_key']['value'], $config['secret_key']['value']);
- if ($token['status'] == 1)return ['status'=>1,'msg' => '密钥验证失败,请检查配置'];
- return $this->checkText($text,$token['token']);
- }
- public function checkText($text,$token){
- $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token='.$token;
- $post_data['text'] = $text;
- $res = $this->post_data($url,$post_data);
- if(isset($res["conclusionType"])){
- if ($res['conclusionType'] == 2){
- if($res['data'][0]['hits'][0]['words']){
- $sensitivearray = implode(',',$res['data'][0]['hits'][0]['words']);
- } else {
- $sensitivearray = '';
- }
- return [
- 'status'=>1,
- 'msg' => $res['data'][0]['msg'].",违规词:".$sensitivearray,
- ];
- }else{
- if ($res['conclusionType'] != 1) \Yii::warning('百度文本检测存在其他报错,报错内容:'.json_encode($res));
- return ['status' => 0];
- }
- }else{
- return ['status' => 0];
- }
- }
- public function getToken($clientId,$clientSecret)
- {
- $cache = new BaiduCache();
- $token = $cache->getCache(self::BAIDUTAXT);
- if (empty($token)) {
- $url = 'https://aip.baidubce.com/oauth/2.0/token';
- $post_data['grant_type'] = 'client_credentials';
- $post_data['client_id'] = $clientId;
- $post_data['client_secret'] = $clientSecret;
- $o = "";
- foreach ($post_data as $k => $v) {
- $o .= "$k=" . urlencode($v) . "&";
- }
- $post_data = substr($o, 0, -1);
- $res = $this->request_post($url, $post_data);
- $res = json_decode($res);
- $res = (array)$res;
- if (!isset($res['access_token'])){
- \Yii::warning('百度文本检测获取token其他报错,报错内容:'.json_encode($res));
- return ['status'=>1];
- }
- $token = $res['access_token'];
- //百度token保存一个月,所以缓存27天更新
- $cache->addCache(self::BAIDUTAXT,$token,2332800);
- }
- return ['status'=>0,'token'=> $cache->getCache(self::BAIDUTAXT)];
- }
- function request_post($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;
- }
- function post_data($url, $param, $return_array = true)
- {
- set_time_limit(0);
- $header [] = "content-type: application/x-www-form-urlencoded";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $res = curl_exec($ch);
- $flat = curl_errno($ch);
- if ($flat) {
- $data = curl_error($ch);
- }
- curl_close($ch);
- $return_array && $res = json_decode($res, true);
- return $res;
- }
- }
|