微信 发布文本内容检测接口msgSecCheck

防止用户通过自己的发布功能发布违规违法不良信息
需要调用微信的msgSecCheck来过滤
官方文档添加链接描述
调用方法在

  //内容安全
  public function content_check($content)
  {
    $content = [
      'content'=>$content
    ];  
    //小程序
    $appid = ''; 
    $secret = '';
    //微信token 
    $token = $this->oauth2Token2($appid,$secret);
    $access_token = $token->access_token;
    $url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token='.$access_token;
    $response = $this->curl_post_weixin($url,$content);      
    return json_decode($response,TRUE);
  }

调用api

  private function curl_post_weixin($url, $data)
  {
    //dump(json_encode($data, JSON_UNESCAPED_UNICODE));
    if($url && count($data)){
      $headers = ['Content-Type'=>'application/json'];
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE)); 
      $res = curl_exec($ch);
      curl_close($ch);
      return $res;
    }
  }

被检测的文本内容必须是json类型的数组,而且不能转义

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));

这一句是核心,普通模式json化就只是json_encode($data),这种方式json化的文本书被转成\u***这种格式,传给接口的相当于是乱码,加了参数JSON_UNESCAPED_UNICODE就不会转义

发布了38 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_35510729/article/details/102801073