收藏
回答

PHP开发调用msgSecCheck,违规内容也返回无问题

框架类型 问题类型 操作系统 工具版本
小程序 Bug Windows 1.02.1808101

通过PHP调用msgSecCheck,无论什么内容均返回OK,无问题,即使是很明显的违规词也返回无问题。

而且看过相关的问题处理,已经是post提交,且验证的内容也是utf-8。


复现demo:


<?php
    $checkContent = '要检测的内容';
    $url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN';
    $data = json_encode(array('content'=>$checkContent));
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_URL,$url); // url
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // json数据
    $res = curl_exec($ch); // 返回值
    curl_close($ch);
    $result = json_decode($res,true);
    
    echo "<pre>";
        var_dump($result);
    echo "</pre>";
?>


最后一次编辑于  2018-08-28
回答关注问题邀请回答
收藏

5 个回答

  • 2018-08-28

    最后结合社区回答解决了问题:

        1、就是必须是post提交;

        2、必须是utf-8编码,这个地方不是对你要验证的内容是utf-8编码,而是你进行json_encode编码时,最后编码的数据是    utf-8编码,原因如下:

        $data = json_encode(array('content'=>$checkContent),JSON_UNESCAPED_UNICODE),然后再调用

    msgSecCheck的api

        JSON_UNESCAPED_UNICODE(中文不转为unicode ,对应的数字 256)

        虽然mb_detect_encoding验证单独的内容已经是utf-8,但是json编码是不使用JSON_UNESCAPED_UNICODE,$data其实是ASCII编码,而非utf-8,就导致验证什么内容均可通过检测


    自己参考资料:

    https://developers.weixin.qq.com/community/develop/doc/000ee4b2a1ccb0c58517ba9b351000?jumpto=reply&parent_commentid=0004e6f9838f58e4ac17e6ef35bc&commentid=0006cc91b9c0481673474b0d25fc


    https://developers.weixin.qq.com/community/develop/doc/000ca2f4dccb60fe2fc6a79bd56c00?highLine=msg_sec_check


    个人代码片段示例:

        /**
         * 微信小程序appid
         */
        const WX_APP_ID = '你的appid';
        
        /**
         * 微信小程序secret
         */
        const WX_APP_SECRET = '你的secret';
        
        /**
         * 微信获取access_token接口地址
         */
        const WX_GET_ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential';
        
        /**
         * 微信验证是否是敏感内容接口地址
         */
        const WX_CHECK_IS_RISKY_CONTENT_URL = 'https://api.weixin.qq.com/wxa/msg_sec_check';



        private $cache;
        
        public function __construct()
        {

            //初始化缓存

            $this->cache = new TestCacheKernel();
        }
        
        /**
         * 获取公众号或小程序access_token
         * @param string $appId
         * @param string $appSecret
         * @return mixed
         */
        public function getWxAccessToken()
        {
            $accessToken = '';
            $cache = new TestCacheKernel();

            //获取缓存的access_token

            $accessToken = $this->cache->getAccessToken($appId);
            if(empty($accessToken))
            {
                $url = self::WX_GET_ACCESS_TOKEN_URL.'&appid='.self::WX_APP_ID.'&secret='.self::WX_APP_SECRET;
                $curl = curl_init();
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_TIMEOUT, 500);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($curl, CURLOPT_URL, $url);
                $res = curl_exec($curl);
                curl_close($curl);           

                $result = json_decode($res,true);
                if(isset($result['errcode']))
                {
                    //TODO::报错code需单独定义
                    $this->throwException('使用appId和secret获取access_token信息出错'.var_export($result,true),1001);
                }
                $accessToken = $result['access_token'];
                $expireTime = $result['expires_in'];

                //设置access_token缓存

                $this->cache->setAccessToken( $accessToken, $expireTime);
            }
            return $accessToken;
        }
        
        /**
         * 验证是否是风险内容
         * @param string $checkContent
         */
        public function checkIsRiskyContent($checkContent)
        {
            $return = false;
            if(!empty($checkContent))
            {
                $access_token = $this->getWxAccessToken();
                $url = self::WX_CHECK_IS_RISKY_CONTENT_URL.'?access_token='.$access_token;
                $data = json_encode(array('content'=>$checkContent),JSON_UNESCAPED_UNICODE);

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_HEADER, FALSE);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($ch, CURLOPT_URL,$url); // url
                curl_setopt($ch, CURLOPT_POST, TRUE);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // json数据
                $dataJson = curl_exec($ch); // 返回值
                curl_close($ch);
                $result= json_decode($dataJson,true)  

           
                if(isset($result['errcode']) && $result['errcode'] == 87014)
                {
                    $return = true;
                }
                elseif(isset($result['errcode']) && $result['errcode'] != 0)
                {
                    //TODO::报错code需单独定义
                    $this->throwException('验证是否是风险内容出错'.var_export($result,true),parent::1002);
                    
                }
            }
            return $return;
        }



    2018-08-28
    有用 1
    回复
  • 嘻嘻哈哈
    嘻嘻哈哈
    2019-10-09

    谢谢楼主解了燃眉之急

    2019-10-09
    有用
    回复
  • 2018-11-15

    楼主好人 这个显然大家都遇到了 官方应该特别说明下

    2018-11-15
    有用
    回复
  • 循
    2018-10-24

    谢谢 楼主

    2018-10-24
    有用
    回复
  • 童虎
    童虎
    2018-08-29

    我也发现这个问题,终于解决了,谢谢楼主

    2018-08-29
    有用
    回复 2
    • .
      .
      2018-09-10

      1、效果 

      不明白他为啥直接返回 php 文件里面的代码?? 而不是返回 php 返回的 json 数据呢? 还望大神赐教,我是后端小白



      2、php文件


      2018-09-10
      回复
    • 童虎
      童虎
      2018-09-14回复.

       json_encode(array('content'=>$checkContent),JSON_UNESCAPED_UNICODE);


      这样写就可以了

      2018-09-14
      回复
登录 后发表内容