ThinkPHP5 如何介入阿里云反文本垃圾敏感词过滤【附源码】

很多时候我们给与客户端在发布内容的时候,就很担心被客户输入恶意敏感词等信息,如若不慎还可能网站被封等;为了减少后台人工审核的成本,我们接入阿里云反文本垃圾。
第一步:在阿里云申请accessKeyId和accessKeySecret
第二步:下载阿里云的检测依赖
阿里云Sdk下载地址:
https://www.alibabacloud.com/help/zh/doc-detail/28440.htm?spm=a2c63.p38356.879954.6.600e905dEygP1j#reference-nh1-nkq-w2b
 
<?php


namespace app\api\controller;
use think\Controller;
use think\Loader;

class Textcheck extends Controller
{
    public function text(){
        Loader::import('aliyunSdk/green-php-sdk-sample/TextScanSample',EXTEND_PATH);
        $sems = new \TextScanSample();
        $content = $this->request->param('words');
        if(!$content){
            return json(['errid' => '0','message'=>'检测字符为空']);
        }
        $res = $sems->textScan($content);
        if($res['suggetion'] == 'pass'){
            return json(['errid' => '0','message'=>'阿里云文本安全审核通过']);
        }else{
            return json(['errid' => '1','message'=>'不通过,原因'.$res['scene']]);
        }
    }

}

类库文件

<?php
/**
 * Created by PhpStorm.
 */

include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;
class TextScanSample {
    public function textScan($content){
        date_default_timezone_set("PRC");
        $config = config('ALIYUN_CONFIG');
        $accessKeyId = $config['ackey'];
        $accessKeySecret = $config['ack_secret'];
        $iClientProfile = DefaultProfile::getProfile("cn-shanghai", $accessKeyId, $accessKeySecret);
        DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        $client = new DefaultAcsClient($iClientProfile);
        $request = new Green\TextScanRequest();
        $request->setMethod("POST");
        $request->setAcceptFormat("JSON");
        $task1 = array('dataId' =>  uniqid(),
            'content' => $content
        );
        $request->setContent(json_encode(array("tasks" => array($task1),
            "scenes" => array("antispam"))));
        try {
            $response = $client->getAcsResponse($request);
            if(200 == $response->code){
                $taskResults = $response->data;
                foreach ($taskResults as $taskResult) {
                    if(200 == $taskResult->code){
                        $sceneResults = $taskResult->results;
                        foreach ($sceneResults as $sceneResult) {
                            $scene = $sceneResult->scene;
                            $suggestion = $sceneResult->suggestion;
                            //根据scene和suggetion做相关的处理
                            //do something
                            $res['scene'] = $scene;
                            $res['suggetion'] = $suggestion;
                            return $res;
                        }
                    }else{
                        $scene= "task process fail:" + $response->code;
                        $res['scene'] = $scene;
                        $res['suggetion'] = 'block';
                        return $res;
                    }
                }
            }else{
                $scene= "detect not success. code:" + $response->code;
                $res['scene'] = $scene;
                $res['suggetion'] = 'block';
                return $res;
            }
        } catch (Exception $e) {
            $res['scene'] = 'ok';
            $res['suggetion'] = $e;
            return $res;
        }
    }

}

  

posted on 2020-11-21 17:55  微凉年前减到115  阅读(519)  评论(0编辑  收藏  举报