搜索
首页php教程php手册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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

热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尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器