PHP 微信公眾號 群發文本訊息
阿新 • • 發佈:2019-01-07
//公眾號 群發文本訊息 //http://api.lialiu.com/test/qunfa public function qunfa(){ //https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN //引入微信工具類 import("ORG.Util.Gongzhonghao"); //例項化 $wx = new weixin; #獲取token $token=$wx->get_token(); #文字訊息 $msgArray= array( "touser"=> array( "", ), "msgtype"=>"text", "text"=>array( "content"=>urlencode("感謝關注賞金聯盟!") ) ); $json=urldecode(json_encode($msgArray)); //var_dump($json);exit; #群發 文字訊息 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$token); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $res = curl_exec($ch); curl_close($ch); if($res['errcode']==0){ echo "推送成功"; }else{ echo "推送失敗!"; } }
使用 urlencode 是為了避免在 json_encode的時候被轉碼unicode.
追加:今天發現使用者收不到群發訊息,於是又去查了文件,發現文件上有這麼一行小字。這意味著,每月只能群發4條訊息。
對於認證服務號雖然開發者使用高階群發介面的每日呼叫限制為100次,但是使用者每月只能接收4條,無論在公眾平臺網站上,還是使用介面群發,使用者每月只能接收4條群發訊息,多於4條的群發將對該使用者傳送失敗;