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

2021-07-08
0

很多时候我们给与客户端在发布内容的时候,就很担心被客户输入恶意敏感词等信息,如若不慎还可能网站被封等;为了减少后台人工审核的成本,我们接入阿里云反文本垃圾。

第一步:在阿里云申请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']]);
        }
    }

}

复制代码

类库文件

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

<?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;

        }

    }

 

}

https://blog.csdn.net/qq_39042037/article/details/109990497

相关信息

评论