小程序内容安全API

  • 检查一段文本是否含有违法违规内容。

POST https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN

  1. //微信内容安全接口
  2. public function wxmsgSecCheck($text)
  3. {
  4. $accessToken = '小程序token';
  5. $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$accessToken;
  6. $data = json_encode(['content'=>$text]);
  7. $respon = $this->curlRequest($url,'post',$data);
  8. $respon = json_decode($respon,true);
  9. if($respon['errcode'] == 87014){
  10. return 1;//效验失败,内容含有违法违规内容
  11. }else{
  12. return 0;
  13. }
  14. }
  15. /*请求外部地址*/
  16. public function curlRequest($url,$mothed = "GET" , $data = array())
  17. {
  18. $ch = curl_init();
  19. $header = "Accept-Charset: utf-8";
  20. curl_setopt($ch, CURLOPT_URL, $url);
  21. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $mothed);
  22. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  23. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  24. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  25. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  26. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  27. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  28. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  29. $temp = curl_exec($ch);
  30. curl_close($ch);
  31. return $temp;
  32. }

校验一张图片是否含有违法违规内容。

POST https://api.weixin.qq.com/wxa/img_sec_check?access_token=ACCESS_TOKEN

  1. public function wximgSecCheck()
  2. {
  3. $url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=".$accessToken;
  4. $imgpath = "/data/default/attachment/images/test.jpg";//这里是图片路径,不能使用远程资源
  5. $imgurl = parse_url($value);//解析url返回组成部分
  6. $imgarr = pathinfo($imgurl['path']);//返回文件路径信息
  7. $data = ['media' => new CURLFile(realpath($imgpath), $imgarr['extension'], $imgarr['filename']),];
  8. $respon = $this->curlRequest($url,'post',$data);
  9. $respon = json_decode($respon,true);
  10. if($respon['errcode'] == 87014){
  11. return 1;//效验失败,内容含有违法违规内容
  12. }else{
  13. return 0;
  14. }
  15. }

参考资料