lavarel框架对接内容过滤接口msg_sec_check接口
发布于 4 年前 作者 ywu 4462 次浏览 来自 分享
经我多次尝试,终于搞定了这个返回都是ok的问题,下面把重要代码放下面,供大家参考,

第一步,获取access_token:

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->app_id}&secret={$this->app_secret}";
$res = json_decode(file_get_contents($url));

$access_token = $res->access_token;

第二步:转发请求post

function curl_post_weixin($url, $data)
{
    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));//注意这里的JSON_UNESCAPED_UNICODE一定要写,不然都是ok
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }
}

第三步:调用转发函数:

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->app_id}&secret={$this->app_secret}";
$res = json_decode(file_get_contents($url));

$access_token = $res->access_token;
$url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=' . $access_token;
// 请求参数
$request = [];
$request['content'] = $content;
$result = curl_post_weixin($url, $request);
return $result;

注意替换自己的appId和token,如果遇到问题不明白的,可以加我微信:xingguangbi

回到顶部