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

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

Jun 14, 2016 am 12:01 AM
php code platform Open source WeChat Push programming programming language subscription software development

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>
Copy after login

 

二、信息发送部分代码:

    <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>
Copy after login

 

三、群发信息代码:

    <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>
Copy after login

 

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

    <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>
Copy after login

 

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

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain strict types (declare(strict_types=1);) in PHP. Explain strict types (declare(strict_types=1);) in PHP. Apr 07, 2025 am 12:05 AM

Strict types in PHP are enabled by adding declare(strict_types=1); at the top of the file. 1) It forces type checking of function parameters and return values ​​to prevent implicit type conversion. 2) Using strict types can improve the reliability and predictability of the code, reduce bugs, and improve maintainability and readability.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) Apr 08, 2025 am 12:03 AM

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

See all articles