[WeChatパブリックアカウント] PHPバージョンはログインをシミュレートし、指定されたユーザーにメッセージをプッシュします
現在、WeChatパブリックアカウントにはオープンなプッシュインターフェイスがありません。模擬ログインとは、ユーザーのメッセージを表示したり、メッセージに返信したりすることができます。 WeChat は禁止されているかどうかを公式に発表していません。過度の使用はアカウント停止などにつながる可能性があります。使用には注意してください。
以下のコードはインターネットから収集したもので、原文が見つからないため、当面はソースを掲載しません。
模擬ログインコード:
<code><span><span><?php</span><span>/* 调用方式简单说明: $arr = array( 'account' => '公众平台帐号', 'password' => '密码' ); $postMsg = new postMsg($arr); $postMsg->getAllUserInfo();//获取所有用户信息 $postMsg->getUserInfo($groupid, $fakeid);//获取所有用户信息,如果默认分组,则$groupid传0 $postMsg->sendMessage('群发内容'); //群发给所有用户 $postMsg->sendMessage('群发内容',$userId); //群发给特定用户,这里的$userId就是用户的fakeid,数组方式传递 */</span><span><span>class</span><span>postMsg</span> {</span><span>public</span><span>$userFakeid</span>;<span>//所有粉丝的fakeid</span><span>private</span><span>$_account</span>;<span>//用户名</span><span>private</span><span>$_password</span>;<span>//密码</span><span>private</span><span>$url</span>;<span>//请求的网址</span><span>private</span><span>$send_data</span>;<span>//提交的数据</span><span>private</span><span>$getHeader</span> = <span>0</span>;<span>//是否显示Header信息</span><span>private</span><span>$token</span>;<span>//公共帐号TOKEN</span><span>private</span><span>$host</span> = <span>'mp.weixin.qq.com'</span>;<span>//主机</span><span>private</span><span>$origin</span> = <span>'https://mp.weixin.qq.com'</span>; <span>private</span><span>$referer</span>;<span>//引用地址</span><span>private</span><span>$cookie</span>; <span>private</span><span>$pageSize</span> = <span>100000</span>;<span>//每页用户数(用于读取所有用户)</span><span>private</span><span>$userAgent</span> = <span>'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'</span>; <span>public</span><span><span>function</span><span>__construct</span><span>(<span>$options</span>)</span>{</span><span>$this</span>->_account = <span>isset</span>(<span>$options</span>[<span>'account'</span>])?<span>$options</span>[<span>'account'</span>]:<span>''</span>; <span>$this</span>->_password = <span>isset</span>(<span>$options</span>[<span>'password'</span>])?<span>$options</span>[<span>'password'</span>]:<span>''</span>; <span>$this</span>->login(); } <span>//登录</span><span>private</span><span><span>function</span><span>login</span><span>()</span>{</span><span>$url</span> = <span>'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN'</span>; <span>$this</span>->send_data = <span>array</span>( <span>'username'</span> => <span>$this</span>->_account, <span>'pwd'</span> => md5(<span>$this</span>->_password), <span>'f'</span> => <span>'json'</span> ); <span>$this</span>->referer = <span>"https://mp.weixin.qq.com/"</span>; <span>$this</span>->getHeader = <span>1</span>; <span>$result</span> = explode(<span>"\n"</span>,<span>$this</span>->curlPost(<span>$url</span>)); <span>echo</span><span>'</br>result'</span>; <span>echo</span><span>$result</span>; <span>foreach</span> (<span>$result</span><span>as</span><span>$key</span> => <span>$value</span>) { <span>$value</span> = trim(<span>$value</span>); <span>if</span> (preg_match(<span>'/token=(\d+)/i'</span>, <span>$value</span>,<span>$match</span>)) { <span>//获取token</span><span>$this</span>->token = trim(<span>$match</span>[<span>1</span>]); } <span>if</span> (preg_match(<span>'/"ret":(.*)/i'</span>, <span>$value</span>,<span>$match</span>)){<span>//获取token</span><span>switch</span> (<span>$match</span>[<span>1</span>]) { <span>case</span> -<span>1</span>: <span>die</span>(json_encode(<span>array</span>(<span>'status'</span>=><span>1</span>,<span>'errCode'</span>=><span>$match</span>[<span>1</span>],<span>'msg'</span>=><span>"系统错误"</span>))); <span>case</span> -<span>2</span>: <span>die</span>(json_encode(<span>array</span>(<span>'status'</span>=><span>1</span>,<span>'errCode'</span>=><span>$match</span>[<span>1</span>],<span>'msg'</span>=><span>"帐号或密码错误"</span>))); <span>case</span> -<span>3</span>: <span>die</span>(urldecode(json_encode(<span>array</span>(<span>'status'</span>=><span>1</span>,<span>'errCode'</span>=><span>$match</span>[<span>1</span>],<span>'msg'</span>=>urlencode(<span>"密码错误"</span>))))); <span>case</span> -<span>4</span>: <span>die</span>(json_encode(<span>array</span>(<span>'status'</span>=><span>1</span>,<span>'errCode'</span>=><span>$match</span>[<span>1</span>],<span>'msg'</span>=><span>"不存在该帐户"</span>))); <span>case</span> -<span>5</span>: <span>die</span>(json_encode(<span>array</span>(<span>'status'</span>=><span>1</span>,<span>'errCode'</span>=><span>$match</span>[<span>1</span>],<span>'msg'</span>=><span>"访问受限"</span>))); <span>case</span> -<span>6</span>: <span>die</span>(json_encode(<span>array</span>(<span>'status'</span>=><span>1</span>,<span>'errCode'</span>=><span>$match</span>[<span>1</span>],<span>'msg'</span>=><span>"需要输入验证码"</span>))); <span>case</span> -<span>7</span>: <span>die</span>(json_encode(<span>array</span>(<span>'status'</span>=><span>1</span>,<span>'errCode'</span>=><span>$match</span>[<span>1</span>],<span>'msg'</span>=><span>"此帐号已绑定私人微信号,不可用于公众平台登录"</span>))); <span>case</span> -<span>8</span>: <span>die</span>(json_encode(<span>array</span>(<span>'status'</span>=><span>1</span>,<span>'errCode'</span>=><span>$match</span>[<span>1</span>],<span>'msg'</span>=><span>"邮箱已存在"</span>))); <span>case</span> -<span>32</span>: <span>die</span>(json_encode(<span>array</span>(<span>'status'</span>=><span>1</span>,<span>'errCode'</span>=><span>$match</span>[<span>1</span>],<span>'msg'</span>=><span>"验证码输入错误"</span>))); <span>case</span> -<span>200</span>: <span>die</span>(json_encode(<span>array</span>(<span>'status'</span>=><span>1</span>,<span>'errCode'</span>=><span>$match</span>[<span>1</span>],<span>'msg'</span>=><span>"因频繁提交虚假资料,该帐号被拒绝登录"</span>))); <span>case</span> -<span>94</span>: <span>die</span>(json_encode(<span>array</span>(<span>'status'</span>=><span>1</span>,<span>'errCode'</span>=><span>$match</span>[<span>1</span>],<span>'msg'</span>=><span>"请使用邮箱登陆"</span>))); <span>case</span><span>10</span>: <span>die</span>(json_encode(<span>array</span>(<span>'status'</span>=><span>1</span>,<span>'errCode'</span>=><span>$match</span>[<span>1</span>],<span>'msg'</span>=><span>"该公众会议号已经过期,无法再登录使用"</span>))); <span>case</span><span>0</span>: <span>$this</span>->userFakeid = <span>$this</span>->getUserFakeid(); <span>break</span>; } } <span>if</span>(preg_match(<span>'/^set-cookie:[\s]+([^=]+)=([^;]+)/i'</span>, <span>$value</span>,<span>$match</span>)){<span>//获取cookie</span><span>$this</span>->cookie .=<span>$match</span>[<span>1</span>].<span>'='</span>.<span>$match</span>[<span>2</span>].<span>'; '</span>; } } } <span>//单发消息</span><span>private</span><span><span>function</span><span>send</span><span>(<span>$fakeid</span>,<span>$content</span>)</span>{</span><span>$url</span> = <span>'https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN'</span>; <span>$this</span>->send_data = <span>array</span>( <span>'type'</span> => <span>1</span>, <span>'f'</span> => <span>'json'</span>, <span>'action'</span> => <span>'sync'</span>, <span>'content'</span> => <span>$content</span>, <span>'tofakeid'</span> => <span>$fakeid</span>, <span>'token'</span> => <span>$this</span>->token, <span>'ajax'</span> => <span>1</span>, ); <span>$this</span>->referer = <span>'https://mp.weixin.qq.com/cgi-bin/singlemsgpage?token='</span>.<span>$this</span>->token.<span>'&fromfakeid='</span>.<span>$fakeid</span>.<span>'&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN'</span>; <span>return</span><span>$this</span>->curlPost(<span>$url</span>); } <span>//群发消息</span><span>public</span><span><span>function</span><span>sendMessage</span><span>(<span>$content</span>=<span>''</span>,<span>$userId</span>=<span>''</span>)</span> {</span><span>if</span>(is_array(<span>$userId</span>) && !<span>empty</span>(<span>$userId</span>)){ <span>foreach</span>(<span>$userId</span><span>as</span><span>$v</span>){ <span>$json</span> = json_decode(<span>$this</span>->send(<span>$v</span>,<span>$content</span>)); <span>if</span>(<span>$json</span>->ret!=<span>0</span>){ <span>$errUser</span>[] = <span>$v</span>; } } }<span>else</span>{ <span>foreach</span>(<span>$this</span>->userFakeid <span>as</span><span>$v</span>){ <span>$json</span> = json_decode(<span>$this</span>->send(<span>$v</span>[<span>'fakeid'</span>],<span>$content</span>)); <span>if</span>(<span>$json</span>->ret!=<span>0</span>){ <span>$errUser</span>[] = <span>$v</span>[<span>'fakeid'</span>]; } } } <span>//共发送用户数</span><span>$count</span> = count(<span>$this</span>->userFakeid); <span>//发送失败用户数</span><span>$errCount</span> = count(<span>$errUser</span>); <span>//发送成功用户数</span><span>$succeCount</span> = <span>$count</span>-<span>$errCount</span>; <span>$data</span> = <span>array</span>( <span>'status'</span>=><span>0</span>, <span>'count'</span>=><span>$count</span>, <span>'succeCount'</span>=><span>$succeCount</span>, <span>'errCount'</span>=><span>$errCount</span>, <span>'errUser'</span>=><span>$errUser</span> ); <span>return</span> json_encode(<span>$data</span>); } <span>//获取所有用户信息</span><span>public</span><span><span>function</span><span>getAllUserInfo</span><span>()</span>{</span><span>foreach</span>(<span>$this</span>->userFakeid <span>as</span><span>$v</span>){ <span>$info</span>[] = <span>$this</span>->getUserInfo(<span>$v</span>[<span>'groupid'</span>],<span>$v</span>[<span>'fakeid'</span>]); } <span>return</span><span>$info</span>; } <span>//获取用户信息</span><span>public</span><span><span>function</span><span>getUserInfo</span><span>(<span>$groupId</span>,<span>$fakeId</span>)</span>{</span><span>$url</span> = <span>"https://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_CN&fakeid={$fakeId}"</span>; <span>$this</span>->getHeader = <span>0</span>; <span>$this</span>->referer = <span>'https://mp.weixin.qq.com/cgi-bin/contactmanagepage?token='</span>.<span>$this</span>->token.<span>'&t=wxm-friend&lang=zh_CN&pagesize='</span>.<span>$this</span>->pageSize.<span>'&pageidx=0&type=0&groupid='</span>.<span>$groupId</span>; <span>$this</span>->send_data = <span>array</span>( <span>'token'</span>=><span>$this</span>->token, <span>'ajax'</span>=><span>1</span> ); <span>$message_opt</span> = <span>$this</span>->curlPost(<span>$url</span>); <span>return</span><span>$message_opt</span>; } <span>//获取所有用户fakeid</span><span>private</span><span><span>function</span><span>getUserFakeid</span><span>()</span>{</span> ini_set(<span>'max_execution_time'</span>,<span>600</span>); <span>$pageSize</span> = <span>1000000</span>; <span>$this</span>->referer = <span>"https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token={$this->token}"</span>; <span>$url</span> = <span>"https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize={$pageSize}&pageidx=0&type=0&groupid=0&token={$this->token}&lang=zh_CN"</span>; <span>$user</span> = <span>$this</span>->vget(<span>$url</span>); var_dump(<span>$user</span>); <span>$preg</span> = <span>"/\"id\":(\d+),\"nick_name\"/"</span>; preg_match_all(<span>$preg</span>,<span>$user</span>,<span>$b</span>); <span>$i</span> = <span>0</span>; <span>foreach</span>(<span>$b</span>[<span>1</span>] <span>as</span><span>$v</span>) { <span>$arr</span>[<span>$i</span>][<span>'fakeid'</span>] = <span>$v</span>; <span>$arr</span>[<span>$i</span>][<span>'groupid'</span>] = <span>0</span>; <span>$i</span>++; } <span>return</span><span>$arr</span>; } <span>/** * curl模拟登录的post方法 *<span> @param</span> $url request地址 *<span> @param</span> $header 模拟headre头信息 *<span> @return</span> json */</span><span>private</span><span><span>function</span><span>curlPost</span><span>(<span>$url</span>)</span> {</span><span>$header</span> = <span>array</span>( <span>'Accept:*/*'</span>, <span>'Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3'</span>, <span>'Accept-Encoding:gzip,deflate,sdch'</span>, <span>'Accept-Language:zh-CN,zh;q=0.8'</span>, <span>'Connection:keep-alive'</span>, <span>'Host:'</span>.<span>$this</span>->host, <span>'Origin:'</span>.<span>$this</span>->origin, <span>'Referer:'</span>.<span>$this</span>->referer, <span>'X-Requested-With:XMLHttpRequest'</span> ); <span>$curl</span> = curl_init(); <span>//启动一个curl会话</span> curl_setopt(<span>$curl</span>, CURLOPT_URL, <span>$url</span>); <span>//要访问的地址</span> curl_setopt(<span>$curl</span>, CURLOPT_HTTPHEADER, <span>$header</span>); <span>//设置HTTP头字段的数组</span> curl_setopt(<span>$curl</span>, CURLOPT_SSL_VERIFYPEER, <span>0</span>); <span>//对认证证书来源的检查</span> curl_setopt(<span>$curl</span>, CURLOPT_SSL_VERIFYHOST, <span>1</span>); <span>//从证书中检查SSL加密算法是否存在</span> curl_setopt(<span>$curl</span>, CURLOPT_USERAGENT, <span>$this</span>->useragent); <span>//模拟用户使用的浏览器</span> curl_setopt(<span>$curl</span>, CURLOPT_FOLLOWLOCATION, <span>1</span>); <span>//使用自动跳转</span> curl_setopt(<span>$curl</span>, CURLOPT_AUTOREFERER, <span>1</span>); <span>//自动设置Referer</span> curl_setopt(<span>$curl</span>, CURLOPT_POST, <span>1</span>); <span>//发送一个常规的Post请求</span> curl_setopt(<span>$curl</span>, CURLOPT_POSTFIELDS, <span>$this</span>->send_data); <span>//Post提交的数据包</span> curl_setopt(<span>$curl</span>, CURLOPT_COOKIE, <span>$this</span>->cookie); <span>//读取储存的Cookie信息</span> curl_setopt(<span>$curl</span>, CURLOPT_TIMEOUT, <span>30</span>); <span>//设置超时限制防止死循环</span> curl_setopt(<span>$curl</span>, CURLOPT_HEADER, <span>$this</span>->getHeader); <span>//显示返回的Header区域内容</span> curl_setopt(<span>$curl</span>, CURLOPT_RETURNTRANSFER, <span>1</span>); <span>//获取的信息以文件流的形式返回</span><span>$result</span> = curl_exec(<span>$curl</span>); <span>//执行一个curl会话</span> curl_close(<span>$curl</span>); <span>//关闭curl</span><span>return</span><span>$result</span>; } <span>private</span><span><span>function</span><span>vget</span><span>(<span>$url</span>)</span>{</span><span>// 模拟获取内容函数</span><span>$header</span> = <span>array</span>( <span>'Accept: */*'</span>, <span>'Connection: keep-alive'</span>, <span>'Host: mp.weixin.qq.com'</span>, <span>'Referer: '</span>.<span>$this</span>->referer, <span>'X-Requested-With: XMLHttpRequest'</span> ); <span>$useragent</span> = <span>'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'</span>; <span>$curl</span> = curl_init(); <span>// 启动一个CURL会话</span> curl_setopt(<span>$curl</span>, CURLOPT_URL, <span>$url</span>); <span>// 要访问的地址</span> curl_setopt(<span>$curl</span>, CURLOPT_HTTPHEADER, <span>$header</span>); <span>//设置HTTP头字段的数组</span> curl_setopt(<span>$curl</span>, CURLOPT_SSL_VERIFYPEER, <span>0</span>); <span>// 对认证证书来源的检查</span> curl_setopt(<span>$curl</span>, CURLOPT_SSL_VERIFYHOST, <span>1</span>); <span>// 从证书中检查SSL加密算法是否存在</span> curl_setopt(<span>$curl</span>, CURLOPT_USERAGENT, <span>$useragent</span>); <span>// 模拟用户使用的浏览器</span> curl_setopt(<span>$curl</span>, CURLOPT_FOLLOWLOCATION, <span>1</span>); <span>// 使用自动跳转</span> curl_setopt(<span>$curl</span>, CURLOPT_AUTOREFERER, <span>1</span>); <span>// 自动设置Referer</span> curl_setopt(<span>$curl</span>, CURLOPT_HTTPGET, <span>1</span>); <span>// 发送一个常规的GET请求</span> curl_setopt(<span>$curl</span>, CURLOPT_COOKIE, <span>$this</span>->cookie); <span>// 读取上面所储存的Cookie信息</span> curl_setopt(<span>$curl</span>, CURLOPT_TIMEOUT, <span>30</span>); <span>// 设置超时限制防止死循环</span> curl_setopt(<span>$curl</span>, CURLOPT_HEADER, <span>$this</span>->getHeader); <span>// 显示返回的Header区域内容</span> curl_setopt(<span>$curl</span>, CURLOPT_RETURNTRANSFER, <span>1</span>); <span>// 获取的信息以文件流的形式返回</span><span>$tmpInfo</span> = curl_exec(<span>$curl</span>); <span>// 执行操作</span><span>if</span> (curl_errno(<span>$curl</span>)) { <span>// echo 'Errno'.curl_error($curl);</span> } curl_close(<span>$curl</span>); <span>// 关闭CURL会话</span><span>return</span><span>$tmpInfo</span>; <span>// 返回数据</span> } } </span></code>
以上、[WeChat パブリックアカウント] PHP 版の模擬ログインと指定ユーザーへのプッシュメッセージを紹介しましたが、PHP チュートリアルに興味のある友人の参考になれば幸いです。