[WeChat Public number]php demo login, push message to specified user currently

Source: Internet
Author: User
Tags preg
[Public]php version of analog login, push messages to specified users

There is currently no open push interface for public numbers. Impersonate the user login, can view the user message, reply the message and so on. The official has not declared whether to prohibit, the heavy use may cause the seal number and so on, uses cautiously!

The following code is collected by the network, can not find the original text, temporarily not affixed to the source.

Impersonation Login Code:


    / * Call method Simple description: $arr = Array (' account ' = ' public Platform accounts ', ' password ' = ' password '); $postMsg = new Postmsg ($arr); $POSTMSG ->getalluserinfo ();//Get all user information $postmsg->getuserinfo ($groupid, $fakeid);//Get all user information, if the default grouping, then $groupid 0$ Postmsg->sendmessage (' mass content '); Mass to all users $postmsg->sendmessage (' mass content ', $userId); Mass to a specific user, where the $userid is the user's Fakeid, the array way to pass * / classpostmsg { Public$userFakeid;//Fakeid of all fansPrivate$_account;//user namePrivate$_password;//PasswordPrivate$url;//Requested URLPrivate$send _data;//Submitted dataPrivate$getHeader=0;//whether to display header informationPrivate$token;//Public account tokenPrivate$host=' mp.weixin.qq.com ';//HostPrivate$origin=' https://mp.weixin.qq.com ';Private$referer;//reference addressPrivate$cookie;Private$pageSize=100000;number of users per page (for all users)Private$userAgent=' mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) gecko/20100101 firefox/23.0 '; Public function__construct($options){$this->_account =isset($options[' account '])?$options[' account ']:'';$this->_password =isset($options[' Password '])?$options[' Password ']:'';$this->login (); }//LoginPrivate functionlogin(){$url=' Https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN ';$this->send_data =Array(' username '=$this->_account,' pwd '= MD5 ($this->_password),' F '=' JSON ');$this->referer ="https://mp.weixin.qq.com/";$this->getheader =1;$result= Explode ("\ n",$this->curlpost ($url));Echo'
Result ';Echo$result;foreach($result as$key=$value) {$value= Trim ($value);if(Preg_match ('/token= (\d+)/I ',$value,$match)) {//Get token$this->token = Trim ($match[1]); }if(Preg_match ('/' ret ':(. *)/I ',$value,$match)){//Get tokenSwitch($match[1]) { Case-1: die(Json_encode (Array(' status '=1,' Errcode '=$match[1],' msg '="System error"))); Case-2: die(Json_encode (Array(' status '=1,' Errcode '=$match[1],' msg '="Incorrect account or password"))); Case-3: die(UrlDecode (Json_encode (Array(' status '=1,' Errcode '=$match[1],' msg '=>urlencode ("Bad password"))))); Case-4: die(Json_encode (Array(' status '=1,' Errcode '=$match[1],' msg '="The account does not exist"))); Case-5: die(Json_encode (Array(' status '=1,' Errcode '=$match[1],' msg '="Access restricted"))); Case-6: die(Json_encode (Array(' status '=1,' Errcode '=$match[1],' msg '="need to enter a verification code"))); Case-7: die(Json_encode (Array(' status '=1,' Errcode '=$match[1],' msg '="This account has a private number and is not available for public platform login"))); Case-8: die(Json_encode (Array(' status '=1,' Errcode '=$match[1],' msg '="Mailbox already exists"))); Case- +: die(Json_encode (Array(' status '=1,' Errcode '=$match[1],' msg '="Verification Code input error"))); Case- $: die(Json_encode (Array(' status '=1,' Errcode '=$match[1],' msg '="The account is denied login due to frequent submission of false information"))); Case-94: die(Json_encode (Array(' status '=1,' Errcode '=$match[1],' msg '="Please use the email login"))); CaseTen: die(Json_encode (Array(' status '=1,' Errcode '=$match[1],' msg '="The public meeting number has expired and can no longer be logged in."))); Case0:$this->userfakeid =$this->getuserfakeid (); Break; } }if(Preg_match ('/^set-cookie:[\s]+ ([^=]+) = ([^;] +) /I ',$value,$match)){//Get Cookies$this->cookie. =$match[1].'='.$match[2].'; '; } } }//Single messagePrivate functionsend($fakeid,$content){$url=' Https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN ';$this->send_data =Array(' type '=1,' F '=' JSON ',' action '=' Sync ',' content '=$content,' Tofakeid '=$fakeid,' token '=$this->token,' Ajax '=1, );$this->referer =' https://mp.weixin.qq.com/cgi-bin/singlemsgpage?token= '.$this->token.' &fromfakeid= '.$fakeid.' &MSGID=&SOURCE=&COUNT=20&T=WXM-SINGLECHAT&LANG=ZH_CN ';return$this->curlpost ($url); }//Mass message Public functionsendMessage($content=",$userId=") {if(Is_array ($userId) &&!Empty($userId)){foreach($userId as$v){$json= Json_decode ($this->send ($v,$content));if($json->ret!=0){$errUser[] =$v; } } }Else{foreach($this->userfakeid as$v){$json= Json_decode ($this->send ($v[' Fakeid '],$content));if($json->ret!=0){$errUser[] =$v[' Fakeid ']; } } }//Total number of users sent$count= Count ($this->userfakeid);//Number of failed send users$errCount= Count ($errUser);//Number of successful users sent$succeCount=$count-$errCount;$data=Array(' status '=0,' Count '=$count,' Succecount '=$succeCount,' ErrCount '=$errCount,' Erruser '=$errUser);returnJson_encode ($data); }//Get all user information Public functiongetalluserinfo(){foreach($this->userfakeid as$v){$info[] =$this->getuserinfo ($v[' GroupID '],$v[' Fakeid ']); }return$info; }//Get user information Public functiongetuserinfo($groupId,$fakeId){$url="https://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_cn&fakeid={$fakeId }";$this->getheader =0;$this->referer =' https://mp.weixin.qq.com/cgi-bin/contactmanagepage?token= '.$this->token.' &t=wxm-friend&lang=zh_cn&pagesize= '.$this->pagesize.' &pageidx=0&type=0&groupid= '.$groupId;$this->send_data =Array(' token '=$this->token,' Ajax '=1);$message _opt=$this->curlpost ($url);return$message _opt; }//Get all users FakeidPrivate functiongetuserfakeid(){Ini_set (' Max_execution_time ', -);$pageSize=1000000;$this->referer ="https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_cn&token={$this->token}";$url="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 ";$user=$this->vget ($url); Var_dump ($user);$preg="/\" id\ ":(\d+), \" nick_name\ "/"; Preg_match_all ($preg,$user,$b);$i=0;foreach($b[1] as$v) {$arr[$i][' Fakeid '] =$v;$arr[$i][' GroupID '] =0;$i++; }return$arr; }/** * Curl Analog Login Post method * @param $url Request address * @param $header Analog Headre header information * @re Turn JSON * /Private functioncurlpost($url) {$header=Array(' accept:*/* ',' accept-charset:gbk,utf-8;q=0.7,*;q=0.3 ',' ACCEPT-ENCODING:GZIP,DEFLATE,SDCH ',' accept-language:zh-cn,zh;q=0.8 ',' connection:keep-alive ',' Host: '.$this->host,' Origin: '.$this->origin,' Referer: '.$this->referer,' X-requested-with:xmlhttprequest ');$curl= Curl_init ();//Start a Curl sessioncurl_setopt ($curl, Curlopt_url,$url);//Address to be accessedcurl_setopt ($curl, Curlopt_httpheader,$header);//Set an array of HTTP header fieldscurl_setopt ($curl, Curlopt_ssl_verifypeer,0);//Check the origin of the certification certificatecurl_setopt ($curl, Curlopt_ssl_verifyhost,1);//Check whether the SSL encryption algorithm exists from the certificatecurl_setopt ($curl, Curlopt_useragent,$this->useragent);//Simulate the browser used by the usercurl_setopt ($curl, Curlopt_followlocation,1);//Use auto jumpcurl_setopt ($curl, Curlopt_autoreferer,1);//Auto Set Referercurl_setopt ($curl, Curlopt_post,1);//Send a regular POST requestcurl_setopt ($curl, Curlopt_postfields,$this->send_data);//post submitted packetscurl_setopt ($curl, Curlopt_cookie,$this->cookie);//Read stored cookie informationcurl_setopt ($curl, Curlopt_timeout, -);//Set timeout limit to prevent dead loopscurl_setopt ($curl, Curlopt_header,$this->getheader);//Display the contents of the header area returnedcurl_setopt ($curl, Curlopt_returntransfer,1);//Gets information returned as a file stream$result= Curl_exec ($curl);//Perform a curl sessionCurl_close ($curl);//Turn off Curlreturn$result; }Private functionvget($url){//Simulation get Content function$header=Array(' Accept: */* ',' connection:keep-alive ',' Host:mp.weixin.qq.com ',' Referer: '.$this->referer,' X-requested-with:xmlhttprequest ');$useragent=' mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) gecko/20100101 firefox/23.0 ';$curl= Curl_init ();//Start a Curl sessioncurl_setopt ($curl, Curlopt_url,$url);//Address to be accessedcurl_setopt ($curl, Curlopt_httpheader,$header);//Set an array of HTTP header fieldscurl_setopt ($curl, Curlopt_ssl_verifypeer,0);//Check the origin of the certification certificatecurl_setopt ($curl, Curlopt_ssl_verifyhost,1);//Check whether the SSL encryption algorithm exists from the certificatecurl_setopt ($curl, Curlopt_useragent,$useragent);//Simulate the browser used by the usercurl_setopt ($curl, Curlopt_followlocation,1);//Use auto jumpcurl_setopt ($curl, Curlopt_autoreferer,1);//Auto Set Referercurl_setopt ($curl, Curlopt_httpget,1);//Send a regular GET requestcurl_setopt ($curl, Curlopt_cookie,$this->cookie);//Read the cookie information stored abovecurl_setopt ($curl, Curlopt_timeout, -);//Set timeout limit to prevent dead loopscurl_setopt ($curl, Curlopt_header,$this->getheader);//Display the contents of the header area returnedcurl_setopt ($curl, Curlopt_returntransfer,1);//Gets information returned as a file stream$tmpInfo= Curl_exec ($curl);//Perform actionsif(Curl_errno ($curl)) {//Echo ' Errno '. Curl_error ($curl);} curl_close ($curl);//Close Curl Sessionreturn$tmpInfo;//Return Data}}

The above describes the [public]php version of the analog login, to the designated users to push the message currently, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.