PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制
2013年10月06日最新整理。
PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制
微信公共平台消息主动推送接口一直是腾讯的私用接口,相信很多朋友都非常想要用到这个功能。
通过学习借鉴朋友的实现方法进行整理,实现了PHP版的微信公共平台消息主动推送。
此方法是通过模拟登录微信公共平台的方法来实现的。
代码如下:
一、登录接口部分代码:
//登录
private function login(){
$url = 'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN';
$this->send_data = array(
'username' => $this->_account,
'pwd' => md5($this->_password),
'f' => 'json'
);
$this->referer = "https://mp.weixin.qq.com/";
$this->getHeader = 1;
$result = explode("\n",$this->curlPost($url));
foreach ($result as $key => $value) {
$value = trim($value);
if(preg_match('/"ErrCode": (.*)/i', $value,$match)){//获取token
switch ($match[1]) {
case -1:
die(json_encode(array('status'=>1,'errCode'=>$match[1],'msg'=>"系统错误")));
case -2:
die(json_encode(array('status'=>1,'errCode'=>$match[1],'msg'=>"帐号或密码错误")));
case -3:
die(urldecode(json_encode(array('status'=>1,'errCode'=>$match[1],'msg'=>urlencode("密码错误")))));
case -4:
die(json_encode(array('status'=>1,'errCode'=>$match[1],'msg'=>"不存在该帐户")));
case -5:
die(json_encode(array('status'=>1,'errCode'=>$match[1],'msg'=>"访问受限")));
case -6:
die(json_encode(array('status'=>1,'errCode'=>$match[1],'msg'=>"需要输入验证码")));
case -7:
die(json_encode(array('status'=>1,'errCode'=>$match[1],'msg'=>"此帐号已绑定私人微信号,不可用于公众平台登录")));
case -8:
die(json_encode(array('status'=>1,'errCode'=>$match[1],'msg'=>"邮箱已存在")));
case -32:
die(json_encode(array('status'=>1,'errCode'=>$match[1],'msg'=>"验证码输入错误")));
case -200:
die(json_encode(array('status'=>1,'errCode'=>$match[1],'msg'=>"因频繁提交虚假资料,该帐号被拒绝登录")));
case -94:
die(json_encode(array('status'=>1,'errCode'=>$match[1],'msg'=>"请使用邮箱登陆")));
case 10:
die(json_encode(array('status'=>1,'errCode'=>$match[1],'msg'=>"该公众会议号已经过期,无法再登录使用")));
case 0:
$this->userFakeid = $this->getUserFakeid();
break;
}
}
if(preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $value,$match)){//获取cookie
$this->cookie .=$match[1].'='.$match[2].'; ';
}
if(preg_match('/"ErrMsg"/i', $value,$match)){//获取token
$this->token = rtrim(substr($value,strrpos($value,'=')+1),'",');
}
}
}
二、信息发送部分代码:
//单发消息
private function send($fakeid,$content){
$url = 'https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN';
$this->send_data = array(
'type' => 1,
'content' => $content,
'error' => 'false',
'tofakeid' => $fakeid,
'token' => $this->token,
'ajax' => 1,
);
$this->referer = 'https://mp.weixin.qq.com/cgi-bin/singlemsgpage?token='.$this->token.'&fromfakeid='.$fakeid.'&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN';
return $this->curlPost($url);
}
三、群发信息代码:
//群发消息
public function sendMessage($content='',$userId='') {
if(is_array($userId) && !empty($userId)){
foreach($userId as $v){
$json = json_decode($this->send($v,$content));
if($json->ret!=0){
$errUser[] = $v;
}
}
}else{
foreach($this->userFakeid as $v){
$json = json_decode($this->send($v['fakeid'],$content));
if($json->ret!=0){
$errUser[] = $v['fakeid'];
}
}
} //共发送用户数
$count = count($this->userFakeid);
//发送失败用户数
$errCount = count($errUser);
//发送成功用户数
$succeCount = $count-$errCount; $data = array(
'status'=>0,
'count'=>$count,
'succeCount'=>$succeCount,
'errCount'=>$errCount,
'errUser'=>$errUser
); return json_encode($data);
}
四、获取所有用户信息代码片段:
//获取所有用户信息
public function getAllUserInfo(){
foreach($this->userFakeid as $v){
$info[] = $this->getUserInfo($v['groupid'],$v['fakeid']);
} return $info;
} //获取用户信息
public function getUserInfo($groupId,$fakeId){
$url = "https://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_CN&fakeid={$fakeId}";
$this->getHeader = 0;
$this->referer = 'https://mp.weixin.qq.com/cgi-bin/contactmanagepage?token='.$this->token.'&t=wxm-friend&lang=zh_CN&pagesize='.$this->pageSize.'&pageidx=0&type=0&groupid='.$groupId;
$this->send_data = array(
'token'=>$this->token,
'ajax'=>1
);
$message_opt = $this->curlPost($url);
return $message_opt;
} //获取所有用户fakeid
private function getUserFakeid(){
ini_set('max_execution_time',600);
$pageSize = 1000000;
$this->referer = "https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token={$_SESSION['token']}";
$url = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize={$pageSize}&pageidx=0&type=0&groupid=0&token={$this->token}&lang=zh_CN";
$user = $this->vget($url);
$preg = "/\"id\":(\d+),\"name\"/";
preg_match_all($preg,$user,$b);
$i = 0;
foreach($b[1] as $v){
$url = 'https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize='.$pageSize.'&pageidx=0&type=0&groupid='.$v.'&token='.$this->token.'&lang=zh_CN';
$user = $this->vget($url);
$preg = "/\"id\":(\d+),\"nick_name\"/";
preg_match_all($preg,$user,$a);
foreach($a[1] as $vv){
$arr[$i]['fakeid'] = $vv;
$arr[$i]['groupid'] = $v;
$i++;
}
}
return $arr;
}
附源代码下载地址(因CSDN资源找不到了所以给出github地址,在写这篇文章的时候是可以用的,但是不保证以后能用):https://github.com/wander112900/wechat
PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制的更多相关文章
- asp.net 实现微信公众平台的主动推送信息
通过学习借鉴朋友的实现方法进行整理(微信公众帐号主动发送消息给用户,asp.net版本). /// <summary> /// MD5 32位加密 /// </summary> ...
- Python 版百度站长平台链接主动推送脚本
如果自己的网站需要被百度收录,可以在搜索结果中找到,就需要将网站的链接提交给百度.依靠百度的爬虫可能无法检索到网站所有的内容,因此可以主动将链接提交给百度. 在百度的站长平台上介绍了链接提交方法,目前 ...
- spring boot 集成 websocket 实现消息主动推送
spring boot 集成 websocket 实现消息主动 前言 http协议是无状态协议,每次请求都不知道前面发生了什么,而且只可以由浏览器端请求服务器端,而不能由服务器去主动通知浏览器端,是单 ...
- php简陋版实现微信公众号主动推送消息
推荐一个网站www.itziy.com csdn免积分下载器.pudn免积分下载器.51cto免积分下载器www.verypan.com 百度网盘搜索引擎www.94cto.com 编程相关视频教程. ...
- 微信公众平台主动推送消息(asp.net)
/// <summary> /// MD5 32位加密 /// </summary> /// <param name=" ...
- ASP.NET MVC 微信公共平台开发之验证消息的真实性
ASP.NET MVC 微信公共平台开发 验证消息的真实性 在MVC Controller所在项目中添加过滤器,在过滤器中重写 public override void OnActionExecuti ...
- 微信公众平台消息接口PHP版开发教程
原文:微信公众平台消息接口PHP版开发教程 一.写好接口程序 在你的服务器上上传好一个接口程序文件,如http://www.yourdomain.com/weixin.php 内容如下: &l ...
- 微信java开发之实现微信主动推送消息
1.拉取access_token2.拉取用户信息3.主动推送消息4.接口貌似要申请权限5.依赖httpclient4.2.3 和jackson 2.2.1 public class WeixinAPI ...
- ASP.NET MVC 微信公共平台开发之获取用户消息并处理
ASP.NET MVC 微信公共平台开发 获取用户消息并处理 获取用户消息 用户发送的消息是在微信服务器发送的一个HTTP POST请求中包含的,获取用户发送的消息要从POST请求的数据流中获取 微信 ...
随机推荐
- Codeforces 703B (模拟) Mishka and trip
题目:这里 题意:n个城市,每个城市有个魅力值vi,首先,有n条路将这n个城市连成一个环,1号城市连2号城市,2号连3号****n号连1号城市,每条路的魅力值是其连接的两个城市 的魅力值的乘积,这n个 ...
- 关于unity3D的学习感想
在老师布置团队项目后组长确定项目是做游戏是,我才接触的Unity3D游戏引擎. 因为一开始我没有接触过这类软件,更没有用过.所以作为一个新手,做好的办 法实在网上找教程.网上说Unity3D是由Uni ...
- ReferenceQueue<T>随笔
参考资料: ReferenceQueue食用手册 java引用食用手册 ReferenceQueue源代码里面很好的展示了java队列的实现思路, 以及多线程观察者的实现思路 多线程观察者实现思路: ...
- PoEdu - C++阶段班【Po学校】- Lesson02_类与对象_第4天
复习:上节作业讲解 注意点: 设计SetString()的时候,要注意重置原来的空间. char * SetString(const char *str) { _len = strlen(str); ...
- 多层嵌套ajax同步
方式一: $.ajax({ type : "post", url : "user/add", data : data, async : false, //必须为 ...
- Mysql命令集
mysql远程授权GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123.com' WITH GRANT OPTION;flush p ...
- linux下的device tree
在我个人的理解,device tree就是描述硬件设备的,目前有什么配置,把这些配置信息告诉linux内核,让内核去识别,增强了内核的通用性,不用因为平台不同而每次都要编译新内核了. 配置device ...
- DBNull 与 求和方法
public int CountMoney() { if (!DBNull.Value.Equals(Eval("LawyerMoney")) && !DBNull ...
- 【章老师的课程】Black Box Testing
本周我们学习了黑盒测试,这是一种常用的软件测试方法,它将被测软件看作一个打不开的黑盒,主要根据功能需求设计测试用例,进行测试.本章主要介绍几种常用的黑盒测试方法和黑盒测试工具,并通过实例介绍各种方法的 ...
- XAMARIN +VS2015 ANDROID 开发判断gps 是否打开。
在获取位置的时候首先要判断gps是否打开,如果没有打开就要提示打开,当然最友好的就是直接调转到打开界面. LocationManager alm = (LocationManager)this.Get ...