php實(shí)現(xiàn)微信公眾號(hào)無(wú)限群發(fā)
利用微信客服接口進(jìn)行各類(lèi)消息的無(wú)限群發(fā)
sendAllMsg.php
<?php
/*
Author:yf
使用說(shuō)明:微信公眾號(hào)無(wú)限群發(fā)接口,使用實(shí)例:
$test = new SendAllMsg("你的appId","你的appSecret");
$test->sendMsgToAll(); //調(diào)用群發(fā)方法
注:1.使用條件:認(rèn)證號(hào)或測(cè)試號(hào)
2.群發(fā)消息內(nèi)容可為圖文、文本、音樂(lè)等,$data具體內(nèi)容參照微信開(kāi)發(fā)文檔/客服接口
3.若用戶(hù)量過(guò)萬(wàn),需修改getUserInfo(),具體參照信開(kāi)發(fā)文檔/獲取關(guān)注者列表
新手上路,大神們多多指點(diǎn),謝謝
*/
interface iSendAllMsg{
function getData($url); //curl 發(fā)送get請(qǐng)求
function postData($url,$data); //curl 發(fā)送post請(qǐng)求
function getAccessToken(); //在構(gòu)造方法中已調(diào)用該方法來(lái)獲取access_token,注意它在wx服務(wù)器的保存時(shí)間7200s
function sendMsgToAll(); //群發(fā)消息方法,發(fā)送的消息$data 可自行修改
}
class SendAllMsg implements iSendAllMsg{
private $appId;
private $appSecret;
private $access_token;
//
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
$this->access_token = $this->getAccessToken();
}
//
function getData($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; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
//
function postData($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; MSIE 5.01; Windows NT 5.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)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
//
function getAccessToken(){
$url = "https://api.weixin.qq.com/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;
}
//
private function getUserInfo(){
$url = "https://api.weixin.qq.com/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;
}
function sendMsgToAll(){
$userInfoList = $this->getUserInfo();
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
foreach($userInfoList as $val){
$data = '{
"touser":"'.$val.'",
"msgtype":"text",
"text":
{
"content":"測(cè)試一下,抱歉打擾各位"
}
}';
$this->postData($url,$data);
}
}
}
$test = new SendAllMsg("YOURappId","YOURappSecret");
$test->sendMsgToall();
?>
以上就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
- 微信支付PHP SDK之微信公眾號(hào)支付代碼詳解
- php實(shí)現(xiàn)微信公眾號(hào)主動(dòng)推送消息
- 微信公眾號(hào)開(kāi)發(fā)之文本消息自動(dòng)回復(fù)php代碼
- 微信公眾號(hào)判斷用戶(hù)是否已關(guān)注php代碼解析
- php判斷用戶(hù)是否關(guān)注微信公眾號(hào)
- Thinkphp微信公眾號(hào)支付接口
- PHP微信公眾號(hào)自動(dòng)發(fā)送紅包API
- 微信公眾號(hào)開(kāi)發(fā)之語(yǔ)音消息識(shí)別php代碼
- PHP版微信公眾平臺(tái)紅包API
- 使用PHP實(shí)現(xiàn)微信搖一搖周邊紅包
- php版微信公眾號(hào)接口實(shí)現(xiàn)發(fā)紅包的方法
- PHP微信公眾號(hào)開(kāi)發(fā)之微信紅包實(shí)現(xiàn)方法分析
相關(guān)文章
php成功操作redis cluster集群的實(shí)例教程
這篇文章主要給大家介紹了關(guān)于php成功操作redis cluster集群的實(shí)例教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
解析:通過(guò)php socket并借助telnet實(shí)現(xiàn)簡(jiǎn)單的聊天程序
本篇文章是對(duì)通過(guò)php socket并借助telnet實(shí)現(xiàn)簡(jiǎn)單聊天程序的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP數(shù)組無(wú)限分級(jí)數(shù)據(jù)的層級(jí)化處理代碼
在很多朋友寫(xiě)無(wú)限級(jí)分類(lèi)數(shù)據(jù)時(shí)都直接使用遞歸來(lái)操作,下面我來(lái)介紹一下關(guān)于PHP無(wú)限分級(jí)代碼優(yōu)化方法,有需要的朋友可參考一下2012-12-12
Thinkphp中import的幾個(gè)用法詳細(xì)介紹
這篇文章主要介紹了Thinkphp中import的幾個(gè)用法,需要的朋友可以參考下2014-07-07
php+ajax實(shí)現(xiàn)圖片文件上傳功能實(shí)例
目前常用的異步文件上傳功能有幾種,比較多見(jiàn)的如使用iframe框架形式,ajax功能效果,以及flash+php功能,需要的朋友可以參考下2014-06-06
php使用PhpSpreadsheet導(dǎo)出Excel表格的實(shí)例詳解
這篇文章將給大家介紹php使用PhpSpreadsheet導(dǎo)出Excel表格的實(shí)例,文中通過(guò)代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01
PHP面向?qū)ο蟪绦蛟O(shè)計(jì)子類(lèi)擴(kuò)展父類(lèi)(子類(lèi)重新載入父類(lèi))操作詳解
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)子類(lèi)擴(kuò)展父類(lèi)(子類(lèi)重新載入父類(lèi))操作,涉及php面向?qū)ο蠓椒ㄖ貙?xiě)與擴(kuò)展相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
flash+php+mysql打造簡(jiǎn)單留言本教程
剛開(kāi)始做這個(gè)留言本的時(shí)候,連mysql和php都沒(méi)接觸過(guò)。經(jīng)過(guò)痛苦的查找資料和學(xué)習(xí),郁悶了一個(gè)星期后完成了我的簡(jiǎn)單留言本2008-07-07

