检测微信小程序输入内容是否合法~

      
    // 获取access_token
    public function _get_access_token() {
        $key = C('DB_NAME') . "_access_token_".C('APP_ID');
        $red = Red::create();

        if ($red->get($key)) {
            return $red->get($key);
        }

        $request_url = "https://api.weixin.qq.com/cgi-bin/token?";
        $request_url .= "grant_type=client_credential&appid=".C('APP_ID')."&secret=".C('APP_SECRET');
        $data = json_decode(Http::doGet($request_url,30),true);

        if ($data['access_token']){
            $red->set($key,$data['access_token'],(int)$data['expires_in']);      
            return $data['access_token'];
        }

        return '';
    }


    /**
     * 检测输入文字内容,是否合法~
     * 
     */
    public function check_input($checkContent){
        $access_token = $this->_get_access_token();
        if(!$access_token){
            $this->json->err('获取access_token失败');
        }

        //$checkContent = '今日犹存免费成人网站,题壁有诗皆抱恨';

        $url = 'https://api.weixin.qq.com/wxa/msg_sec_check?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数据
        $res = curl_exec($ch); // 返回值
        curl_close($ch);
        $result = json_decode($res,true);
        
        if($result['errcode'] !== 0){
            $this->json->err('内容非法-' . $result['errmsg']);
        }
    }


原文地址:https://www.cnblogs.com/pansidong/p/13260863.html