php实现微信公众号群发消息接口(thinkphp3.2.3)
一、说明1、可以在前一篇博客基础上添加一个方法即可,方法代码下面讲解二、实现流程1、看开发手册: //获取access_tokenfunction getAccessToken(){
$appid = 'your_appid';
$appsecret = 'your_appsecret';
$url = ' $res = $result = json_decode($res, true);
$access_token = $result['access_token'];
return $access_token;
}
//发送 $curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
//群发文本消息function sendTextMsgAll(){
$access_token = $this->getAccessToken();
$url = ' $data = '{
"filter":{
"is_to_all":true },
"text":{
"content":"群发的文本消息"
},
"msgtype":"text"
}';
$res = $this-> $data);
return $res;
}
//群发图片消息function sendImageMsgAll(){
$access_token = $this->getAccessToken();
$url = ' $data = '{
"filter":{
"is_to_all":true },
"image":{
"media_id":"your_media_id"
},
"msgtype":"image"
}';
$res = $this-> $data);
return $res;
}
//群发图文消息function sendNewsMsgAll(){
$access_token = $this->getAccessToken();
$url = ' $data = '{
"filter":{
"is_to_all":true },
"mpnews":{
"media_id":"your_media_id"
},
"msgtype":"mpnews"
}';
$res = $this-> $data);
return $res;
}
//发送 $data){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
```
以上代码中,我们实现了获取access_token、群发文本消息、图片消息、图文消息等功能。在getAccessToken方法中,我们通过调用微信提供的接口获取到有效的access_token;在sendTextMsgAll、sendImageMsgAll、sendNewsMsgAll方法中,我们实现了向关注公众号的用户群发文本消息、图片消息、图文消息的功能;在