首页 php教程 php手册 PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息

PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息

Jun 06, 2016 pm 07:47 PM
php 一天 平台 推送 消息 突破 订阅

2013年10月06日最新整理。 PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制 微信公共平台消息主动推送接口一直是腾讯的私用接口,相信很多朋友都非常想要用到这个功能。 通过学习借鉴朋友的实现方法进行整理,实现了PHP版的微信公共平台

2013年10月06日最新整理。

PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制

微信公共平台消息主动推送接口一直是腾讯的私用接口,相信很多朋友都非常想要用到这个功能。

通过学习借鉴朋友的实现方法进行整理,实现了PHP版的微信公共平台消息主动推送。

此方法是通过模拟登录微信公共平台的方法来实现的。

代码如下:

一、登录接口部分代码:

    <span>//</span><span>登录</span>
    <span>private</span> <span>function</span><span> login(){
        </span><span>$url</span> = 'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN'<span>;
        </span><span>$this</span>->send_data = <span>array</span><span>(
            </span>'username' => <span>$this</span>->_account,
            'pwd' => <span>md5</span>(<span>$this</span>->_password),
            'f' => 'json'<span>
        );
        </span><span>$this</span>->referer = "https://mp.weixin.qq.com/"<span>;
        </span><span>$this</span>->getHeader = 1<span>;
        </span><span>$result</span> = <span>explode</span>("\n",<span>$this</span>->curlPost(<span>$url</span><span>));
        </span><span>foreach</span> (<span>$result</span> <span>as</span> <span>$key</span> => <span>$value</span><span>) {
            </span><span>$value</span> = <span>trim</span>(<span>$value</span><span>);
            </span><span>if</span>(<span>preg_match</span>('/"ErrCode": (.*)/i', <span>$value</span>,<span>$match</span>)){<span>//</span><span>获取token</span>
                <span>switch</span> (<span>$match</span>[1<span>]) {
                    </span><span>case</span> -1:
                        <span>die</span>(json_encode(<span>array</span>('status'=>1,'errCode'=><span>$match</span>[1],'msg'=>"系统错误"<span>)));
                    </span><span>case</span> -2:
                        <span>die</span>(json_encode(<span>array</span>('status'=>1,'errCode'=><span>$match</span>[1],'msg'=>"帐号或密码错误"<span>)));
                    </span><span>case</span> -3:
                        <span>die</span>(<span>urldecode</span>(json_encode(<span>array</span>('status'=>1,'errCode'=><span>$match</span>[1],'msg'=><span>urlencode</span>("密码错误"<span>)))));
                    </span><span>case</span> -4:
                        <span>die</span>(json_encode(<span>array</span>('status'=>1,'errCode'=><span>$match</span>[1],'msg'=>"不存在该帐户"<span>)));
                    </span><span>case</span> -5:
                        <span>die</span>(json_encode(<span>array</span>('status'=>1,'errCode'=><span>$match</span>[1],'msg'=>"访问受限"<span>)));
                    </span><span>case</span> -6:
                        <span>die</span>(json_encode(<span>array</span>('status'=>1,'errCode'=><span>$match</span>[1],'msg'=>"需要输入验证码"<span>)));
                    </span><span>case</span> -7:
                        <span>die</span>(json_encode(<span>array</span>('status'=>1,'errCode'=><span>$match</span>[1],'msg'=>"此帐号已绑定私人微信号,不可用于公众平台登录"<span>)));
                    </span><span>case</span> -8:
                        <span>die</span>(json_encode(<span>array</span>('status'=>1,'errCode'=><span>$match</span>[1],'msg'=>"邮箱已存在"<span>)));
                    </span><span>case</span> -32:
                        <span>die</span>(json_encode(<span>array</span>('status'=>1,'errCode'=><span>$match</span>[1],'msg'=>"验证码输入错误"<span>)));
                    </span><span>case</span> -200:
                        <span>die</span>(json_encode(<span>array</span>('status'=>1,'errCode'=><span>$match</span>[1],'msg'=>"因频繁提交虚假资料,该帐号被拒绝登录"<span>)));
                    </span><span>case</span> -94:
                        <span>die</span>(json_encode(<span>array</span>('status'=>1,'errCode'=><span>$match</span>[1],'msg'=>"请使用邮箱登陆"<span>)));
                    </span><span>case</span> 10:
                        <span>die</span>(json_encode(<span>array</span>('status'=>1,'errCode'=><span>$match</span>[1],'msg'=>"该公众会议号已经过期,无法再登录使用"<span>)));
                    </span><span>case</span> 0:
                        <span>$this</span>->userFakeid = <span>$this</span>-><span>getUserFakeid();
                        </span><span>break</span><span>;
                }
            }
            </span><span>if</span>(<span>preg_match</span>('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', <span>$value</span>,<span>$match</span>)){<span>//</span><span>获取cookie</span>
                <span>$this</span>->cookie .=<span>$match</span>[1].'='.<span>$match</span>[2].'; '<span>;
            }
            </span><span>if</span>(<span>preg_match</span>('/"ErrMsg"/i', <span>$value</span>,<span>$match</span>)){<span>//</span><span>获取token</span>
                <span>$this</span>->token = <span>rtrim</span>(<span>substr</span>(<span>$value</span>,<span>strrpos</span>(<span>$value</span>,'=')+1),'",'<span>);
            }
        }
    }</span>
登录后复制

 

二、信息发送部分代码:

    <span>//</span><span>单发消息</span>
    <span>private</span> <span>function</span> send(<span>$fakeid</span>,<span>$content</span><span>){
        </span><span>$url</span> = 'https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN'<span>;
        </span><span>$this</span>->send_data = <span>array</span><span>(
                </span>'type' => 1,
                'content' => <span>$content</span>,
                'error' => 'false',
                'tofakeid' => <span>$fakeid</span>,
                'token' => <span>$this</span>->token,
                'ajax' => 1,<span>
            );
        </span><span>$this</span>->referer = 'https://mp.weixin.qq.com/cgi-bin/singlemsgpage?token='.<span>$this</span>->token.'&fromfakeid='.<span>$fakeid</span>.'&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN'<span>;
        </span><span>return</span> <span>$this</span>->curlPost(<span>$url</span><span>);
    }</span>
登录后复制

 

三、群发信息代码:

    <span>//</span><span>群发消息</span>
    <span>public</span> <span>function</span> sendMessage(<span>$content</span>='',<span>$userId</span>=''<span>) {
        </span><span>if</span>(<span>is_array</span>(<span>$userId</span>) && !<span>empty</span>(<span>$userId</span><span>)){
            </span><span>foreach</span>(<span>$userId</span> <span>as</span> <span>$v</span><span>){
                </span><span>$json</span> = json_decode(<span>$this</span>->send(<span>$v</span>,<span>$content</span><span>));
                </span><span>if</span>(<span>$json</span>->ret!=0<span>){
                    </span><span>$errUser</span>[] = <span>$v</span><span>;
                }
            }
        }</span><span>else</span><span>{
            </span><span>foreach</span>(<span>$this</span>->userFakeid <span>as</span> <span>$v</span><span>){
                </span><span>$json</span> = json_decode(<span>$this</span>->send(<span>$v</span>['fakeid'],<span>$content</span><span>));
                </span><span>if</span>(<span>$json</span>->ret!=0<span>){
                    </span><span>$errUser</span>[] = <span>$v</span>['fakeid'<span>];
                }
            }
        }
        
        </span><span>//</span><span>共发送用户数</span>
        <span>$count</span> = <span>count</span>(<span>$this</span>-><span>userFakeid);
        </span><span>//</span><span>发送失败用户数</span>
        <span>$errCount</span> = <span>count</span>(<span>$errUser</span><span>);
        </span><span>//</span><span>发送成功用户数</span>
        <span>$succeCount</span> = <span>$count</span>-<span>$errCount</span><span>;
        
        </span><span>$data</span> = <span>array</span><span>(
            </span>'status'=>0,
            'count'=><span>$count</span>,
            'succeCount'=><span>$succeCount</span>,
            'errCount'=><span>$errCount</span>,
            'errUser'=><span>$errUser</span><span> 
        );
        
        </span><span>return</span> json_encode(<span>$data</span><span>);
    }</span>
登录后复制

 

四、获取所有用户信息代码片段:

    <span>//</span><span>获取所有用户信息</span>
    <span>public</span> <span>function</span><span> getAllUserInfo(){
        </span><span>foreach</span>(<span>$this</span>->userFakeid <span>as</span> <span>$v</span><span>){
            </span><span>$info</span>[] = <span>$this</span>->getUserInfo(<span>$v</span>['groupid'],<span>$v</span>['fakeid'<span>]);
        }
        
        </span><span>return</span> <span>$info</span><span>;
    }
    
    
    
    </span><span>//</span><span>获取用户信息</span>
    <span>public</span> <span>function</span> getUserInfo(<span>$groupId</span>,<span>$fakeId</span><span>){
        </span><span>$url</span> = "https://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_CN&fakeid={<span>$fakeId</span>}"<span>;
        </span><span>$this</span>->getHeader = 0<span>;
        </span><span>$this</span>->referer = 'https://mp.weixin.qq.com/cgi-bin/contactmanagepage?token='.<span>$this</span>->token.'&t=wxm-friend&lang=zh_CN&pagesize='.<span>$this</span>->pageSize.'&pageidx=0&type=0&groupid='.<span>$groupId</span><span>;
        </span><span>$this</span>->send_data = <span>array</span><span>(
            </span>'token'=><span>$this</span>->token,
            'ajax'=>1<span>
        );
        </span><span>$message_opt</span> = <span>$this</span>->curlPost(<span>$url</span><span>);
        </span><span>return</span> <span>$message_opt</span><span>;
    }
    
    </span><span>//</span><span>获取所有用户fakeid</span>
    <span>private</span> <span>function</span><span> getUserFakeid(){
        </span><span>ini_set</span>('max_execution_time',600<span>);
        </span><span>$pageSize</span> = 1000000<span>;
        </span><span>$this</span>->referer = "https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token={<span>$_SESSION</span>['token']}"<span>;
        </span><span>$url</span> = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize={<span>$pageSize</span>}&pageidx=0&type=0&groupid=0&token={<span>$this</span>->token}&lang=zh_CN"<span>;
        </span><span>$user</span> = <span>$this</span>->vget(<span>$url</span><span>);
        </span><span>$preg</span> = "/\"id\":(\d+),\"name\"/"<span>;
        </span><span>preg_match_all</span>(<span>$preg</span>,<span>$user</span>,<span>$b</span><span>);
        </span><span>$i</span> = 0<span>;
        </span><span>foreach</span>(<span>$b</span>[1] <span>as</span> <span>$v</span><span>){
            </span><span>$url</span> = 'https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize='.<span>$pageSize</span>.'&pageidx=0&type=0&groupid='.<span>$v</span>.'&token='.<span>$this</span>->token.'&lang=zh_CN'<span>;
            </span><span>$user</span> = <span>$this</span>->vget(<span>$url</span><span>);
            </span><span>$preg</span> = "/\"id\":(\d+),\"nick_name\"/"<span>;
            </span><span>preg_match_all</span>(<span>$preg</span>,<span>$user</span>,<span>$a</span><span>);
            </span><span>foreach</span>(<span>$a</span>[1] <span>as</span> <span>$vv</span><span>){
                </span><span>$arr</span>[<span>$i</span>]['fakeid'] = <span>$vv</span><span>;
                </span><span>$arr</span>[<span>$i</span>]['groupid'] = <span>$v</span><span>;
                </span><span>$i</span>++<span>;
            }
        }
        </span><span>return</span> <span>$arr</span><span>;
    }</span>
登录后复制

 

附源代码下载地址(因CSDN资源找不到了所以给出github地址,在写这篇文章的时候是可以用的,但是不保证以后能用):https://github.com/wander112900/wechat

 

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

CakePHP 项目配置 CakePHP 项目配置 Sep 10, 2024 pm 05:25 PM

在本章中,我们将了解CakePHP中的环境变量、常规配置、数据库配置和电子邮件配置。

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

在本章中,我们将学习以下与路由相关的主题?

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

CakePHP 使用数据库 CakePHP 使用数据库 Sep 10, 2024 pm 05:25 PM

在 CakePHP 中使用数据库非常容易。本章我们将了解CRUD(创建、读取、更新、删除)操作。

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

See all articles