WeChat development test, letter development test

Source: Internet
Author: User

Test Development and Test Development

<? Php/* usage: $ arr = array ('account' => 'public platform account', 'Password' => 'Password '); $ w = new Weixin ($ arr); $ w-> getAllUserInfo (); // get user information $ w-> sendMessage ('group content '); // send Group messages to all users $ w-> sendMessage ('group content ', $ userId); // send Group messages to specific users */class Weixin {public $ userFakeid; // fakeid private $ _ account for all fans; // username private $ _ password; // password private $ url; // request url private $ send_data; // The submitted data is private $ getHeader = 0; // whether the Header information is private $ token; // public account T OKEN private $ 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 '; public function _ construct ($ options) {$ this-> _ account = isset ($ options ['account'])? $ Options ['account']: ''; $ this-> _ password = isset ($ options ['Password'])? $ Options ['Password']: ''; $ this-> login ();} // 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_encod E (array ('status' => 1, 'errcode' => $ 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 ("Incorrect password"); 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, 'errcode' => $ match [1], 'msg '=> "this account has been bound with a private number and cannot be used for public platform Logon"); 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"); case-200: die (json_encode (array (' Status '=> 1, 'errorcode' => $ match [1], 'msg' => "this account is denied due to frequent submission of false information "))); 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 )) {// get cookie $ this-> cook Ie. = $ match [1]. '= '. $ match [2]. ';} if (preg_match ('/"ErrMsg"/I ', $ value, $ match )) {// get token $ this-> token = rtrim (substr ($ value, strrpos ($ value, '=') + 1 ),'",');}}} // 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 );} // 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);} // obtain all user information. public function getAllUserInfo () {foreach ($ this-> userFakeid as $ v) {$ info [] = $ t His-> getUserInfo ($ v ['groupid'], $ v ['fakeid']);} return $ info;} // obtain 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: // response? 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;}/*** curl: post method for simulating logon * @ param $ url request address * @ param $ header simulate headre header information * @ return json */priva Te function curlPost ($ 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 session curl_setopt ($ curl, CURLOPT_URL, $ url); // curl_setop T ($ 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); // 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 curl_setopt ($ curl, CURLOPT_HEADER, $ this-> getHeader); // display the returned Header region 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); // disable curl return $ result;} private function vget ($ url) {// simulate the function for obtaining content $ header = array ('Accept :*/*', 'Accept-Encoding: gzip, deflate', 'Accept-Language: zh-CN, zh; q = 100', '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 Use a CURL session curl_setopt ($ curl, CURLOPT_URL, $ url); // the address to be accessed curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ header ); // set the array of the HTTP header field curl_setopt ($ 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 sets Referer curl_setopt ($ curl, CURLOPT_HTTPGET, 1); // sends 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 as a file stream $ tmpInfo = curl_exec ($ curl); // the operation if (curl _ Errno ($ curl) {// echo 'errno '. curl_error ($ curl);} curl_close ($ curl); // close the CURL session return $ tmpInfo; // return data }}$ arr = array ("account" => "xxxxx@qq.com", "password" => "xxxxx"); $ w = new Weixin ($ arr ); $ info = $ w-> getAllUserInfo (); // obtain the user information var_dump ($ info);?>


Why development?

Follow suit! It's like there were a lot of people playing 51 games in the past. No one playing now. What a stranger is, it's just a bit of a float cloud, so it doesn't make much sense to use it or else you feel like you can't keep up with the trend, one QQ is enough. Now QQ can make voice calls! Anyway, I am useless. I will not be idle and put two software with the same functions on the mobile phone. What's more, the software is growing and the memory is not enough.
 
How do I enter the public development mode URL?

This url is used to verify whether the message you are processing comes from the server,

When a developer submits a Verification Application for the first time, the server sends a GET request to the URL filled in with four parameters (signature, timestamp, nonce, echostr ), the developer verifies the signature to determine the authenticity of the message.
After that, each time a developer receives a user message, the previous three parameters (signature, timestamp, and nonce) will be included to access the URL set by the developer, the developer still checks the authenticity of the message by verifying the signature. The verification method is the same as the verification application submitted for the first time.

The developer verifies the request by verifying signature (The following is a verification method ). If you confirm that the GET request is from the server, return the content of the echostr parameter as it is, the access takes effect and becomes a developer. Otherwise, the access fails.

The encryption/verification process is as follows: 1. sort the tokens, timestamp, and nonce in Lexicographic Order. splice the three parameter strings into one string for sha1 encryption. the encrypted string obtained by the developer can be compared with signature to identify that the request comes from

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.