zoukankan      html  css  js  c++  java
  • Demo-网易易盾敏感字检测

     
    语言:php
    使用方法:
      需要先通过 https://dun.163.com/ 申请并开通内容安全检测的产品
    为什么用这个:
      因为网络敏感字一直在不断地更新和叠加,不通过第三方检测机构的话,自己很难去维护,稍微不注意就有漏网之鱼,现在做互联网产品的太难了,什么都需要小心翼翼。
     
      1 <?php
      2 /** 产品密钥ID,产品标识 */
      3 define("SECRETID", "");
      4 /** 产品私有密钥,服务端生成签名信息使用,请严格保管,避免泄露 */
      5 define("SECRETKEY", "");
      6 /** 业务ID,易盾根据产品业务特点分配 */
      7 define("BUSINESSID", "");
      8 /** 易盾反垃圾云服务文本在线检测接口地址 */
      9 define("API_URL", "http://as.dun.163.com/v3/text/check");
     10 /** api version */
     11 define("VERSION", "v3.1");
     12 /** API timeout*/
     13 define("API_TIMEOUT", 2);
     14 /** php内部使用的字符串编码 */
     15 define("INTERNAL_STRING_CHARSET", "auto");
     16 /** api signatureMethod,默认MD5,支持国密SM3 */
     17 define("SIGNATURE_METHOD", "MD5");
     18 
     19 /**
     20  * curl post请求
     21  * @params 输入的参数
     22  */
     23 function curl_post($params, $url, $timout){
     24     $ch = curl_init();
     25     curl_setopt($ch, CURLOPT_URL, $url);
     26     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     27     // 设置超时时间
     28     curl_setopt($ch, CURLOPT_TIMEOUT, $timout);
     29     // POST数据
     30     curl_setopt($ch, CURLOPT_POST, 1);
     31     // 把post的变量加上
     32     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
     33     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.'application/x-www-form-urlencoded; charset=UTF-8'));
     34     $output = curl_exec($ch);
     35     curl_close($ch);
     36     return $output;
     37 }
     38 
     39 /**
     40  * 将输入数据的编码统一转换成utf8
     41  * @params 输入的参数
     42  */
     43 function toUtf8($params){
     44     $utf8s = array();
     45     foreach ($params as $key => $value) {
     46         $utf8s[$key] = is_string($value) ? mb_convert_encoding($value, "utf8", INTERNAL_STRING_CHARSET) : $value;
     47     }
     48     return $utf8s;
     49 }
     50 
     51 /**
     52  * 计算参数签名
     53  * $params 请求参数
     54  * $secretKey secretKey
     55  */
     56 function gen_signature($secretKey, $params){
     57     $params["signatureMethod"] == SIGNATURE_METHOD;
     58     ksort($params);
     59     $buff="";
     60     foreach($params as $key=>$value){
     61          if($value !== null) {
     62             $buff .=$key;
     63         $buff .=$value;
     64              }
     65     }
     66     $buff .= $secretKey;
     67     return md5($buff);
     68     if ($params["signatureMethod"] == "SM3") {
     69         return sm3($buff);
     70     } else {
     71         return md5($buff);
     72     }
     73 }
     74 
     75 
     76 /**
     77  * 反垃圾请求接口简单封装
     78  * $params 请求参数
     79  */
     80 function check($params){
     81     $params["secretId"] = SECRETID;
     82     $params["businessId"] = BUSINESSID;
     83     $params["version"] = VERSION;
     84     $params["timestamp"] = time() * 1000;// time in milliseconds
     85     $params["nonce"] = sprintf("%d", rand()); // random int
     86 
     87     $params = toUtf8($params);
     88     $params["signature"] = gen_signature(SECRETKEY, $params);
     89     // var_dump($params);
     90 
     91     $result = curl_post($params, API_URL, API_TIMEOUT);
     92     if($result === FALSE){
     93         return array("code"=>500, "msg"=>"file_get_contents failed.");
     94     }else{
     95         return json_decode($result, true);    
     96     }
     97 }
     98 
     99 $check_content = $_POST['check_content'];
    100 if (empty($check_content)) 
    101 {
    102     $arr =  array('status' => 'FAIL', 'msg'=>'检测内容不能为空' );
    103     echo json_encode($arr);exit();
    104 }
    105 
    106 $params = array(
    107     "dataId"    => "id".time().rand(),
    108     "content"    =>$check_content
    109 );
    110 
    111 $ret = check($params);
    112 if ($ret["code"] == 200) 
    113 {
    114     $action = $ret["result"]["action"];
    115     $taskId = $ret["result"]["taskId"];
    116     $labelArray = $ret["result"]["labels"];
    117 
    118     if ($action == 0) 
    119     {
    120         //echo "taskId={$taskId},文本机器检测结果:通过
    ";
    121         $arr =  array('status' => 'SUCCESS', 'msg'=>'检测通过' );
    122         echo json_encode($arr);exit();
    123     } 
    124     else if ($action == 1) 
    125     {
    126           //echo "taskId={$taskId},文本机器检测结果:嫌疑,需人工复审,分类信息如下:".json_encode($labelArray)."
    ";
    127           $arr =  array('status' => 'FAIL', 'msg'=>'检测不通过,需人工复审' );
    128         echo json_encode($arr);exit();
    129     } 
    130     else if ($action == 2) 
    131     {
    132         //echo "taskId={$taskId},文本机器检测结果:不通过,分类信息如下:".json_encode($labelArray)."
    ";
    133         $arr =  array('status' => 'FAIL', 'msg'=>'检测不通过' );
    134         echo json_encode($arr);exit();
    135     }
    136 }
    137 else
    138 {
    139     $arr =  array('status' => 'FAIL', 'msg'=>'检测不通过' );
    140     echo json_encode($arr);exit();
    141 }
    142 
    143 
    144 ?>

    调用方法:

    通过post请求,传入 check_content 参数,将会返回 json数据
    status值为 ‘SUCCESS’ 表示检测通过
    status值为 ‘FAIL’ 表示检测不通过
     

    其他说明:

    易盾策略会一直跟新,根据最新的监管知识和时下热点
     
    咱们的智能审核平台支持自主添加策略:
    1、需要拦截的“关键词”:https://cms.dun.163.com/strategy/keyword;
    2、需要避开检测的“忽略词”:https://cms.dun.163.com/strategy/ignoreword;
    3、相似/精确匹配语句特征:
    https://cms.dun.163.com/strategy/text-feature;
    4、黑白用户ID名单
    等等……
     
    请登录易盾智能审核系统(https://cms.dun.163.com),您可以在[系统管理——产品管理——查看详情——获取凭证]中获取秘钥信息,在[系统管理——业务管理]中获取业务ID。
    秘钥信息是用来做产品标识及接口签名使用的,请注意保密。
    详情页的业务ID供内容检查使用,businessId参数为接口公共参数之一,每个请求必须包含(***解决方案类产品不需要***)。
    参考文档在这里:http://support.dun.163.com/
    demo可以在这里下载:http://support.dun.163.com/documents/2018041901?docId=150426959377256448
    如果您是线上业务数据接入,为了提升防控效果,请您将用户ID(account)、用户ip(IP)字段传给我们。我们会为您配置行为检测模型,对广告、灌水等类别能多维度防控。
     

    但行好事,莫问前程!

    本文来自博客园,作者:yangphp,转载请注明原文链接:https://www.cnblogs.com/ypeih/p/15259224.html

  • 相关阅读:
    别让猴子翻到背上
    python生成二维码
    50条经典爱情观
    智力测试题
    SQL数据库优化
    递归函数实现二分查找法
    软件开发类别
    递归函数的深度问题
    UVALive
    UVALive
  • 原文地址:https://www.cnblogs.com/ypeih/p/15259224.html
Copyright © 2011-2022 走看看