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

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

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version
