通过学习借鉴朋友的实现方法进行整理,实现了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;

}

希望本文所述对大家学习php程序设计有所帮助。

php订阅号发送消息,php实现微信公众号主动推送消息相关推荐

  1. 公众号向特定用户主动推送消息_SAP系统和微信集成的系列教程之三:微信用户关注公众号之后,自动在SAP C4C系统创建客户主数据...

    这是Jerry 2020年的第84篇文章,也是汪子熙公众号总共第266篇原创文章. 本系列的英文版Jerry写作于2017年,这个教程总共包含十篇文章,发表在SAP社区上: https://blogs ...

  2. PHP实现微信公众号主动推送消息

    这篇文章主要介绍了php实现微信公众号主动推送消息的方法,PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制,需要的朋友可以参考下. 通过学习借鉴朋友的实现方法进行整理,实现了PHP ...

  3. 发送报警信息到微信公众号

    #!/bin/sh # 微信消息发送脚本 #全局配置-- #微信公众号appID appID=xxxxxxxxxxxxxx #微信公众号appsecret appsecret=xxxxxxxxxxxx ...

  4. 微信公众号-java上传图片素材到微信服务器基于事件推送两条消息

    微信公众号-java上传图片素材到微信服务器&基于事件推送两条消息 场景:基于微信扫码登录/关注事件,向用户推送两条消息. 分析:翻阅了微信开发者文档和一些其他资料发现,公众号针对用户关注/扫 ...

  5. php 主动推送消息,php实现微信公众号主动推送消息

    通过学习借鉴朋友的实现方法进行整理,实现了PHP版的微信公共平台消息主动推送,分享给大家供大家参考,具体内容如下 此方法是通过模拟登录微信公共平台的方法来实现的. 代码如下: 一.登录接口部分代码 / ...

  6. php 公众号验证回调方法_微信公众号关键词自动回复设置方法!

    什么是公众号关键词自动回复? 在微信公众号平台设置关键词自动回复,可以通过添加规则,关注/订阅的用户发送的消息内容如果是你设置的关键字,即可以实现自动回复预先设置好的内容. 关键字自动回复设置方法: ...

  7. 【微信公众号】如何开通一个微信公众号

    [微信公众号]如何开通一个微信公众号 文章目录 [微信公众号]如何开通一个微信公众号 一.微信公众号申请时可能存在的问题 二.微信公众号中已发布的图文如何修改为已群发 三.如何在微信公众号中写mark ...

  8. 微信公众号数据2019_如何制作微信公众号图文素材 微信公众号采集器好用吗

    现在有很多人都会通过微信公众号来发布文章.图片,这时候就需要使用一些编辑技巧了.下面拓途数据就和大家一同来看看如何制作微信公众号图文素材,微信公众号采集器好用吗? 微信公众号图文素材 如何制作微信公众 ...

  9. 微信公众号迁移丨如何迁移微信公众号 最详细公众号迁移流程和方法

    微信公众号迁移丨如何迁移微信公众号 最详细微信公众号迁移流程和方法 微信公众号如何迁移?微信公众号迁移公证书需要准备哪些资料? 怎么迁移微信公众号! 教你迁移微信公众号_迁移完成后,原账号就注销了,无 ...

  10. MT4/MT5 EA通过微信,QQ实时推送消息的实现

    这篇为本人在CSDN原创文章,转载请注明 MT4/MT5很多人是通过邮件来进行信息推送,慢,不稳定,容易漏报. 相信很多人在寻找通过微信,QQ实时推送消息的方法,我自己也网上找了很久没有找到. 腾讯对 ...

最新文章

  1. SVO 学习笔记(深度滤波)
  2. [IDEA 配置MYSQL数据库连接]
  3. 数据库建立索引、数据表创建规则、备用字段 / 保留字段 / 预留字段
  4. 2016-05-09的POC Yaas Open Event的代码审查
  5. 理解 OpenStack 高可用(HA) (6): MySQL HA
  6. 吴恩达机器学习 11.聚类
  7. Android 面试那些事儿
  8. 群晖NAS教程(十五)、利用Web Station安装typecho博客
  9. android 图标删除不了,手机程序卸载之后桌面上残留的图标删除不掉
  10. 百度地图api 在3D地图中设置自定义样式
  11. Something's wrong--perhaps a missing \item. \end{thebibliography}
  12. spring-security实现权限管理
  13. 使用POI提取Word文件的内容(纯文本、带html格式)
  14. 俞优静的“航海”人生-搜狐财经
  15. 世界顶尖青年发明家:13华人上榜[其中5人毕业于中科大]
  16. lottie android 源码,Lottie动画库 Android 端源码浅析
  17. (15.1.5.2)当抗日神剧遇上轻小说(我已笑抽)
  18. 路由器重温——接口配置与管理2
  19. 分位数和分位线(Quantiles and Percentiles)
  20. Java 2 实用教程(第五版)课后习题-【编程题答案】

热门文章

  1. 《无主之地2》已经完工,内容已整理完整封盘送审
  2. 安装VS2022后报错 _CRT_SECURE_NO_WARNINGS
  3. 在线教育机构如何运营微信公众号
  4. Android OpenCV应用篇三:提取图片中的文字
  5. 如何提取ppt模板?
  6. 安装tensorflow报错:Cannot uninstall ‘wrapt‘. It is a distutils installed project and thus we cannot accu
  7. WPS如何将金额快速改为万元显示
  8. H3C新华三链路聚合介绍
  9. linux切换ip地址脚本,批处理实现的ip地址切换的复杂脚本
  10. 拉依达准则python实现