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)

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.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

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.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles