匹配是否含有敏感词三种方法

方法一:
/**敏感词过滤 (没有匹配到关键字就返回 false)
     * @param $content
     * @return bool
     */
    public static function sensitiveWord($content)
    {
        $fake = file_get_contents(public_path('sensitive/all.txt')); // 读取关键字文本信息 这个文本就是敏感词的文本网上都有 最好选择最新最全的
        $content = trim(trim($content), ','); //一定要去除左右空格  最好就是把所有特殊符号也去掉
        $fuckArr = explode("\n", $fake); // 把关键字转换为数组

        for ($i = 0; $i < count($fuckArr); $i++) {
            if ($fuckArr[$i] == "") {
                continue; //如果关键字为空就跳过本次循环
            }
//            dd($content, trim(trim($fuckArr[$i]),','));
            if (strpos($content, trim(trim($fuckArr[$i]), ',')) !== false) {
                $r = $fuckArr[$i]; //如果匹配到关键字就返回关键字
                return true;
            }
        }
        return false; // 如果没有匹配到关键字就返回 false
    }
/********敏感*词过滤************/
$vailTitle = ValueUtil::sensitiveWord($payload->getTitle());
if ($vailTitle) {
    throw new CustomErrorMessageException('标题含有敏感词');
}

敏感词链接:https://pan.baidu.com/s/1i1FepvCbIH1LuevmUZPWTQ 
提取码:yehw 
 

 

方法二:调用微信官方文档接口:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html#method-http

 public static function get_access_token()
    {
        try {
            $access_token = \Cache::get('CACHE_UTIL_TOKEN');
            if (!$access_token) {
                $client = new \GuzzleHttp\Client();
                $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxx&secret=xxx';
                $response = $client->request('GET', $url);
                $result = $response->getBody()->getContents();
                $data = json_decode($result, true);
                \Log::info($data);
                $access_token = $data['access_token'];
                if ($access_token) {
                    \Cache::add("CACHE_UTIL_TOKEN", $access_token, 7200 / 60);
                } else {
                    \Cache::forget('CACHE_UTIL_TOKEN');
                    ValueUtil::get_access_token();
                }
            }
            return $access_token;
        } catch (\Throwable $exception) {
            \Log::error($exception);
        }

    }
控制器中调用获取token:
$content = '港独';
$token = ValueUtil::get_access_token();
$client = new \GuzzleHttp\Client();
$data = json_encode(['content' => $content], JSON_UNESCAPED_UNICODE);
$url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" . $token;
$response = $client->request('POST', $url, [
        'body' => $data
    ]
);
$statusCode = $response->getStatusCode();
$result = $response->getBody()->getContents();
$data = json_decode($result, true);
return $data;

方法三EasyWechat文本安全内容检测

$app = Factory::miniProgram($config);
//$config 配置详见:https://www.easywechat.com/docs/master/mini-program/index
// 传入要检测的文本内容,长度不超过500K字节
$content = '你好';

$result = $app->content_security->checkText($content);
dd($result);
// 正常返回 0
{
    "errcode": "0",
    "errmsg": "ok"
}

//当 $content 内含有敏感信息,则返回 87014
{
    "errcode": 87014,
    "errmsg": "risky content"
}

 

 

 

 

 

 

你可能感兴趣的:(日常笔记)