[WeChat official account] php simulated login, push messages to specified users currently

Source: Internet
Author: User
Tags preg
: This article mainly introduces the [public account] php version of the simulated login, to push messages to a specified user currently, for PHP Tutorial interested, you can refer. [Public Account] simulates logon in php to push messages to specified users

Currently, the public account does not have an open push interface. Simulated logon simulates user logon and allows you to view user messages and reply to messages. The official team did not state whether it is forbidden. a large amount of use may lead to a block number, etc. use it with caution!

The following code is collected by the network. the source text cannot be found.

Simulated logon code:


  'Public platform account', 'password' => 'password'); $ postMsg = new postMsg ($ arr); $ postMsg-> getAllUserInfo (); // get all user information $ postMsg-> getUserInfo ($ groupid, $ fakeid); // Obtain all user information, if the default group, then $ groupid transmits 0 $ postMsg-> sendMessage ('group content '); // send the message to all users in a group: $ postMsg-> sendMessage ('group content', $ userId ); // send the group to a specific user. $ userId here is the user's fakeid, which is transmitted in array mode */classpostMsg {public $ userFakeid; // fakeidprivate $ _ account of all fans; // username private $ _ password; // password private $ url; // requested url private $ send_data; // submitted data private $ getHeader = 0; // whether the Header information is private $ token; // public account TOKENprivate $ host = 'MP .weixin.qq.com '; // host private $ origin =' https://mp.weixin.qq.com '; Private $ referer; // reference address private $ cookie; private $ pageSize = 100000; // Number of users per page (used to read all users) private $ userAgent = 'mozilla/5.0 (Windows NT 6.1; WOW64; rv: 23.0) Gecko/20100101 Firefox/23.0 '; publicfunction _ construct ($ options) {$ this-> _ account = isset ($ options ['account'])? $ Options ['account']: ''; $ this-> _ password = isset ($ options ['password'])? $ Options ['password']: ''; $ this-> login ();} // log on to privatefunctionlogin () {$ 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 ($ resultas $ 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, 'errorcode' => $ match [1], 'MSG '=> "system error "))); case-2: die (json_encode (array ('status' => 1, 'errorcode' => $ match [1], 'MSG '=> "incorrect account or password"); case-3: die (urldecode (json_encode (array ('status' => 1, 'errorcode' => $ match [1], 'MSG '=> urlencode ("password error"); case-4: die (json_encode (array ('status' => 1, 'errorcode' => $ match [1], 'MSG '=> "This account does not exist "))); case-5: die (json_encode (array ('status' => 1, 'errorcode' => $ match [1], 'MSG '=> "restricted access"); case-6: die (json_encode (array ('status' => 1, 'errorcode' => $ match [1], 'MSG '=> "verification code required"); case-7: die (json_encode (array ('status' => 1, 'errorcode' => $ match [1], 'MSG '=> "This account has been bound to a private account, cannot be used for public platform login "); case-8: die (json_encode (array ('status' => 1, 'errorcode' => $ match [1], 'MSG '=> "email already exists"); case-32: die (json_encode (array ('status' => 1, 'errorcode' => $ match [1], 'MSG '=> "incorrect verification code input"); case-200: die (json_encode (array ('status' => 1, 'errorcode' => $ match [1], 'MSG '=> "due to frequent submission of false information, this account is denied to log on "); case-94: die (json_encode (array ('status' => 1, 'errorcode' => $ match [1], 'MSG '=> "please log on via email"); case10: die (json_encode (array ('status' => 1, 'errcode' => $ match [1], 'MSG '=> "This public meeting number has expired and cannot be used again"); case0: $ this-> userFakeid = $ this-> getUserFakeid (); break;} if (preg_match ('/^ set-cookie: [\ s] + ([^ =] +) = ([^;] +)/I ', $ value, $ match )) {// Obtain cookie $ this-> cookie. = $ match [1]. '= '. $ match [2]. ';' ;}}// send message privatefunctionsend ($ 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 );} // send a group message publicfunctionsendMessage ($ content = '', $ userId ='') {if (is_array ($ userId )&&! Empty ($ userId) {foreach ($ userIdas $ 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 sent users $ count = count ($ this-> userFakeid ); // number of failed users $ errCount = count ($ errUser); // number of successfully sent users $ succeCount = $ count-$ errCount; $ data = array ('status' => 0, 'Count' => $ count, 'succecount' => $ succeCount, 'errcount' => $ errCount, 'erruser' => $ errUser ); return json_encode ($ data);} // Obtain all user information publicfunctiongetAllUserInfo () {foreach ($ this-> userFakeid as $ v) {$ info [] = $ this-> getUserInfo ($ v ['groupid'], $ v ['fakeid']);} return $ info ;} // obtain the user information publicfunctiongetUserInfo ($ 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 fakeidprivatefunctiongetUserFakeid () {ini_set ('max _ execution_time ', 600 ); $ 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 ;} /*** post method of curl simulated logon * @ param $ url request address * @ param $ header simulate headre header information * @ return json */privatefunctioncurlPost ($ url) {$ header = array ('accept: */* ', 'Accept-Charset: GBK, UTF-8; q = 0.7, *; q = 100', 'Accept-Encoding: gzip, deflate, sdch ', 'Accept-Language: zh-CN, zh; q = 000000', 'connection: keep-alive', 'host :'. $ this-> host, 'Origin :'. $ this-> origin, 'referer :'. $ this-> referer, 'X-Requested-With: XMLHttpRequest '); $ curl = curl_init (); // Start a curl session curl_setopt ($ curl, CURLOPT_URL, $ url); // The address to be accessed curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ header); // Set the array curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0) of the HTTP header field ); // Check the certificate source curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check from the certificate whether the SSL encryption algorithm has curl_setopt ($ curl, CURLOPT_USERAGENT, $ this-> useragent); // simulate the user's browser curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic jump curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1 ); // automatically set Referer curl_setopt ($ curl, CURLOPT_POST, 1); // send a regular Post request curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ this-> send_data ); // Post the submitted data packet curl_setopt ($ curl, CURLOPT_COOKIE, $ this-> cookie); // read the stored Cookie information curl_setopt ($ curl, CURLOPT_TIMEOUT, 30 ); // Set the timeout limit to prevent endless loops curl_setopt ($ curl, CURLOPT_HEADER, $ this-> getHeader); // display the returned Header area content curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // The obtained information is returned as a file stream $ result = curl_exec ($ curl); // execute a curl session curl_close ($ curl); // close curlreturn $ result ;} privatefunctionvget ($ url) {// simulate the function of getting content $ 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 session curl_setopt ($ curl, CURLOPT_URL, $ url ); // The URL curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ header) to be accessed; // Set the array curl_setopt of the HTTP header field ($ curl, CURLOPT_SSL_VERIFYPEER, 0 ); // Check the certificate source curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check from the certificate whether the SSL encryption algorithm has curl_setopt ($ curl, CURLOPT_USERAGENT, $ useragent ); // simulate the user's browser curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic jump curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1 ); // automatically set Referer curl_setopt ($ curl, CURLOPT_HTTPGET, 1); // send a regular GET request curl_setopt ($ curl, CURLOPT_COOKIE, $ this-> cookie ); // read the previously stored Cookie information curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // Set the timeout limit to prevent endless loops curl_setopt ($ curl, CURLOPT_HEADER, $ this-> getHeader); // display the returned Header region content curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // The obtained information is returned in the form of a file stream $ tmpInfo = curl_exec ($ curl); // execute the operation if (curl_errno ($ curl) {// echo 'errno '. curl_error ($ curl);} curl_close ($ curl); // close CURL session return $ tmpInfo; // return data }}

The above introduces the [public account] php version of the simulated login, to push messages to a specified user currently, including the content, hope to be helpful to friends interested in PHP tutorials.

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.