Use php to actively push messages to WeChat official accounts

Source: Internet
Author: User
Tags learn php programming preg
: This article describes how to actively push messages to the public account in php. For more information about PHP tutorials, see. By learning from the implementation methods of friends, we have achieved active message push on the public platform of PHP version 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:
I. 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 the public function sendMessage ($ c {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 ;}

I hope this article will help you learn php programming.

The above section describes how to actively push messages to the public account in php, including content, and hope to help those who are 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.