关于小程序接入敏感词检测接口的坑
接入 msgSecCheck 接口47001 错误码踩坑!
这是官方文档要求,写的有些笼统,根据开发者社区提供需要进行编码后在传参,以下为 php 代码示例
/** * 敏感词检测 * @param $str * @return mixed * @throws \app\common\exception\BaseException * @throws \think\Exception * @throws \think\exception\DbException */ protected function msgSecCheck($str) { $wxConfig = WxappModel::getWxappCache(); $access_token = (new WxBase($wxConfig['app_id'], $wxConfig['app_secret']))->getAccessToken(); // 获取access_token $data = ['content' => $str]; // 主要就是这个 json_encode($data, JSON_UNESCAPED_UNICODE) 来处理传参问题,与请求头关系不大 $result = curlPost('https://api.weixin.qq.com/wxa/msg_sec_check?access_token=' . $access_token, json_encode($data, JSON_UNESCAPED_UNICODE)); $result = json_decode($result, true); if ($result['errcode'] === 0) { return true; } return false; } /** * curl请求指定url (post) * @param $url * @param array $data * @return mixed */ function curlPost($url, $data = []) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); curl_close($ch); return $result; }
测试用例文本:
特3456书yuuo莞6543李zxcz蒜7782法fgnv级 完2347全dfji试3726测asad感3847知qwez到
若接口errcode返回87014(内容含有违法违规内容),则对接成功。
评论