By learning from the implementation methods of friends, we have achieved active message push on the public platform and shared it with you for your reference, the specific content is as follows: This method is implemented by simulating logon to the public platform. By learning from the implementation methods of friends, we have achieved proactive message push on the public platform and shared it with you for your reference. the specific content is as follows:
This method is implemented by simulating logon to the public platform.
The code is as follows:
1. http://www.php.cn/php/php-TVOS-denglu.html "target =" _ blank "> logon interface code
// Log on to the private function login () {$ 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 )); foreach ($ result as $ key => $ value) {$ value = trim ($ value); if (preg_match ('/"ErrCode ":(. *)/I ', $ value, $ match) {// Get token switch ($ 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"); case 10: die (json_encode (array ('status' => 1, 'errcode' => $ match [1], 'MSG '=> "This public meeting number has expired and cannot be used again"); case 0: $ this-> userFakeid = $ this-> getUserFakeid (); break;} if (preg_match ('/^ set-cookie: [\ s] + ([^ =] +) = ([^;] +)/I ', $ value, $ match )) {// Obtain cookie $ this-> cookie. = $ match [1]. '= '. $ match [2]. ';} if (preg_match ('/"ErrMsg"/I ', $ value, $ match )) {// Get token $ this-> token = rtrim (substr ($ value, strrpos ($ value, '=') + 1 ),'",');}}}
II. code for sending information
// Private function send ($ fakeid, $ content) {$ url = 'https: // mp.weixin.qq.com/cgi-bin/singlesend? T = ajax-response & lang = zh_CN '; $ this-> send_data = array ('type' => 1, 'content' => $ content, 'Error' => 'false', '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 );}
3. code for sending group information
// Send public function sendMessage ($ 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 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 );}
4. get all user information code snippets
// Obtain all user information. public function getAllUserInfo () {foreach ($ this-> userFakeid as $ v) {$ info [] = $ this-> getUserInfo ($ v ['groupid'], $ v ['fakeid']);} return $ info ;} // get user information public function getUserInfo ($ 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;} // Obtain all users' fakeid private function getUserFakeid () {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= {$ _ SESSION ['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); $ preg = "/\" id \ ":( \ d +), \" name \"/"; preg_match_all ($ preg, $ user, $ B); $ I = 0; foreach ($ B [1] as $ v) {$ url =' https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize= '. $ PageSize. '& pageidx = 0 & type = 0 & groupid = '. $ v. '& token = '. $ this-> token. '& lang = zh_CN'; $ user = $ this-> vget ($ url); $ preg = "/\" id \ ":( \ d + ), \ "nick_name \"/"; preg_match_all ($ preg, $ user, $ a); foreach ($ a [1] as $ vv) {$ arr [$ I] ['fakeid'] = $ vv; $ arr [$ I] ['groupid'] = $ v; $ I ++ ;}} return $ arr ;}
The above is the details about how to actively push messages to the public account. For more information, see other related articles on php Chinese network!