利用php怎么实现一个微信公众号无限群发功能
发布时间:-01-25 17:32:14
来源:亿速云
阅读:66
作者:Leah
今天就跟大家聊聊有关利用php怎么实现一个微信公众号无限群发功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
sendAllMsg.php<?php
interfaceiSendAllMsg{
functiongetData($url);//curl发送get请求
functionpostData($url,$data);//curl发送post请求
functiongetAccessToken();//在构造方法中已调用该方法来获取access_token,注意它在wx服务器的保存时间7200s
functionsendMsgToAll();//群发消息方法,发送的消息$data可自行修改
}
classSendAllMsgimplementsiSendAllMsg{
private$appId;
private$appSecret;
private$access_token;
//
publicfunction__construct($appId,$appSecret){
$this->appId=$appId;
$this->appSecret=$appSecret;
$this->access_token=$this->getAccessToken();
}
//
functiongetData($url){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0(compatible;MSIE5.01;WindowsNT5.0)');
curl_setopt($ch,CURLOPT_ENCODING,'gzip');
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$data=curl_exec($ch);
curl_close($ch);
return$data;
}
//
functionpostData($url,$data){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0(compatible;MSIE5.01;WindowsNT5.0)');
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_AUTOREFERER,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$tmpInfo=curl_exec($ch);
if(curl_errno($ch)){
returncurl_error($ch);
}
curl_close($ch);
return$tmpInfo;
}
//
functiongetAccessToken(){
$url="https://api./cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret;
$res=$this->getData($url);
$jres=json_decode($res,true);
$access_token=$jres['access_token'];
return$access_token;
}
//
privatefunctiongetUserInfo(){
$url="https://api./cgi-bin/user/get?access_token=".$this->access_token;
$res=$this->getData($url);
$jres=json_decode($res,true);
//print_r($jres);
$userInfoList=$jres['data']['openid'];
return$userInfoList;
}
functionsendMsgToAll(){
$userInfoList=$this->getUserInfo();
$url="https://api./cgi-bin/message/custom/send?access_token=".$this->access_token;
foreach($userInfoListas$val){
$data='{
"touser":"'.$val.'",
"msgtype":"text",
"text":
{
"content":"测试一下,抱歉打扰各位"
}
}';
$this->postData($url,$data);
}
}
}
$test=newSendAllMsg("YOURappId","YOURappSecret");
$test->sendMsgToall();
?>
看完上述内容,你们对利用php怎么实现一个微信公众号无限群发功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。