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

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

WBOY
WBOY原創
2016-06-14 00:01:451660瀏覽

2013年10月06日最新整理。

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

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

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

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

代码如下:

一、登录接口部分代码:

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

 

二、信息发送部分代码:

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

 

三、群发信息代码:

    <span style="color: #008000;">//</span><span style="color: #008000;">群发消息</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> sendMessage(<span style="color: #800080;">$content</span>='',<span style="color: #800080;">$userId</span>=''<span style="color: #000000;">) {
        </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$userId</span>) && !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$userId</span><span style="color: #000000;">)){
            </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$userId</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$v</span><span style="color: #000000;">){
                </span><span style="color: #800080;">$json</span> = json_decode(<span style="color: #800080;">$this</span>->send(<span style="color: #800080;">$v</span>,<span style="color: #800080;">$content</span><span style="color: #000000;">));
                </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$json</span>->ret!=0<span style="color: #000000;">){
                    </span><span style="color: #800080;">$errUser</span>[] = <span style="color: #800080;">$v</span><span style="color: #000000;">;
                }
            }
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$this</span>->userFakeid <span style="color: #0000ff;">as</span> <span style="color: #800080;">$v</span><span style="color: #000000;">){
                </span><span style="color: #800080;">$json</span> = json_decode(<span style="color: #800080;">$this</span>->send(<span style="color: #800080;">$v</span>['fakeid'],<span style="color: #800080;">$content</span><span style="color: #000000;">));
                </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$json</span>->ret!=0<span style="color: #000000;">){
                    </span><span style="color: #800080;">$errUser</span>[] = <span style="color: #800080;">$v</span>['fakeid'<span style="color: #000000;">];
                }
            }
        }
        
        </span><span style="color: #008000;">//</span><span style="color: #008000;">共发送用户数</span>
        <span style="color: #800080;">$count</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">userFakeid);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">发送失败用户数</span>
        <span style="color: #800080;">$errCount</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$errUser</span><span style="color: #000000;">);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">发送成功用户数</span>
        <span style="color: #800080;">$succeCount</span> = <span style="color: #800080;">$count</span>-<span style="color: #800080;">$errCount</span><span style="color: #000000;">;
        
        </span><span style="color: #800080;">$data</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
            </span>'status'=>0,
            'count'=><span style="color: #800080;">$count</span>,
            'succeCount'=><span style="color: #800080;">$succeCount</span>,
            'errCount'=><span style="color: #800080;">$errCount</span>,
            'errUser'=><span style="color: #800080;">$errUser</span><span style="color: #000000;"> 
        );
        
        </span><span style="color: #0000ff;">return</span> json_encode(<span style="color: #800080;">$data</span><span style="color: #000000;">);
    }</span>

 

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

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

 

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

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn