WeChat scan logon-php Tutorial

Source: Internet
Author: User
Scan logon
Users can obtain login information by scanning the QR code provided by the webpage.

  1. /**
  2. * Public platform PHP-SDK
  3. * Wechatauth is an unofficial API login
  4. * Users can obtain login information by scanning the QR code provided by the webpage.
  5. * Mainly implements the following functions:
  6. * Get_login_code () obtains the logon authorization code. only the two-dimensional code can be obtained through the authorization code.
  7. * Get_code_image ($ code = '') converts the authorization code obtained above to the image QR code
  8. * Verify_code () indicates whether the logon is successful. if 200 is returned, the final authorization is successful.
  9. * After the get_login_cookie () authentication is successful, call this method to obtain basic user information.
  10. * SendNews ($ account, $ title, $ summary, $ content, $ pic, $ srcurl = '') sends graphic information to an account
  11. * Get_avatar ($ url) obtains the user's profile picture data
  12. * @ Author dodge
  13. * [Url = home. php? Mod = space & uid = 17823] @ LINK [/url] https://github.com/dodgepudding/wechat-php-sdk
  14. * @ Version 1.1
  15. *
  16. */
  17. Include "snoopy. class. php ";
  18. Class Wechatauth
  19. {
  20. Private $ cookie;
  21. Private $ _ cookiename;
  22. Private $ _ cookieexpired = 3600;
  23. Private $ _ account = 'test ';
  24. Private $ _ datapath = './data/cookie _';
  25. Private $ debug;
  26. Private $ _ logcallback;
  27. Public $ login_user; // The current login user, which is obtained after get_login_info is called.
  28. Public function _ construct ($ options)
  29. {
  30. $ This-> _ account = isset ($ options ['account'])? $ Options ['account']: '';
  31. $ This-> _ datapath = isset ($ options ['datapath'])? $ Options ['datapath']: $ this-> _ datapath;
  32. $ This-> debug = isset ($ options ['debug'])? $ Options ['debug']: false;
  33. $ This-> _ logcallback = isset ($ options ['logcallback'])? $ Options ['logcallback']: false;
  34. $ This-> _ cookiename = $ this-> _ datapath. $ this-> _ account;
  35. $ This-> getCookie ($ this-> _ cookiename );
  36. }
  37. /**
  38. * Write the cookie to the cache.
  39. * @ Param string $ filename cache file name
  40. * @ Param string $ content file content
  41. * @ Return bool
  42. */
  43. Public function saveCookie ($ filename, $ content ){
  44. Return file_put_contents ($ filename, $ content );
  45. }
  46. /**
  47. * Read cookie cache content
  48. * @ Param string $ filename cache file name
  49. * @ Return string cookie
  50. */
  51. Public function getCookie ($ filename ){
  52. If (file_exists ($ filename )){
  53. $ Mtime = filemtime ($ filename );
  54. If ($ mtime _ Cookieexpired) return false;
  55. $ Data = file_get_contents ($ filename );
  56. If ($ data) $ this-> cookie = $ data;
  57. }
  58. Return $ this-> cookie;
  59. }
  60. /*
  61. * Delete a cookie
  62. */
  63. Public function deleteCookie ($ filename ){
  64. $ This-> cookie = '';
  65. @ Unlink ($ filename );
  66. Return true;
  67. }
  68. Private function log ($ log ){
  69. If ($ this-> debug & function_exists ($ this-> _ logcallback )){
  70. If (is_array ($ log) $ log = print_r ($ log, true );
  71. Return call_user_func ($ this-> _ logcallback, $ log );
  72. }
  73. }
  74. /**
  75. * Obtain the authorization code corresponding to the login QR code
  76. */
  77. Public function get_login_code (){
  78. If ($ this-> _ logincode) return $ this-> _ logincode;
  79. $ T = time (). strval (mt_rand (100,999 ));
  80. $ Codeurl = 'https: // login.weixin.qq.com/jslogin? Appid = Signature & redirect_uri = https % 3A % 2F % 2Fwx.qq.com % 2Fcgi-bin % 2Fmmwebwx-bin % 2 Fwebwxnewloginpage & fun = new & lang = zh_CN & _ = '. $ t;
  81. $ Send_snoopy = new Snoopy;
  82. $ Send_snoopy-> fetch ($ codeurl );
  83. $ Result = $ send_snoopy-> results;
  84. If ($ result ){
  85. Preg_match ("/window. QRLogin. uuid \ s + = \ s + \" ([^ \ "] +) \"/", $ result, $ matches );
  86. If (count ($ matches)> 1 ){
  87. $ This-> _ logincode = $ matches [1];
  88. $ _ SESSION ['login _ step'] = 0;
  89. Return $ this-> _ logincode;
  90. }
  91. }
  92. Return $ result;
  93. }
  94. /**
  95. * Obtain the corresponding QR code image address using the authorization code
  96. * @ Param string $ code
  97. * @ Return string image url
  98. */
  99. Public function get_code_image ($ code = ''){
  100. If ($ code = '') $ code = $ this-> _ logincode;
  101. If (! $ Code) return false;
  102. Return 'http: // login.weixin.qq.com/qrcode/'.w.this-> _ logincode .'? T = webwx ';
  103. }
  104. /**
  105. * Set the authorization code corresponding to the QR code
  106. * @ Param string $ code
  107. * @ Return class $ this
  108. */
  109. Public function set_login_code ($ code ){
  110. $ This-> _ logincode = $ code;
  111. Return $ this;
  112. }
  113. /**
  114. * Qr code login verification
  115. *
  116. * @ Return status:
  117. *> = 400: invaild code; 408: not auth and wait, 400,401: not valid or expired
  118. * 201: just scaned but not confirm
  119. * 200: confirm then you can get user info
  120. */
  121. Public function verify_code (){
  122. If (! $ This-> _ logincode) return false;
  123. $ T = time (). strval (mt_rand (100,999 ));
  124. $ Url = 'https: // login.weixin.qq.com/cgi-bin/mmwebwx-bin/login? Uuid = '. $ this-> _ logincode.' & tip = 1 & _ = '. $ t;
  125. $ Send_snoopy = new Snoopy;
  126. $ Send_snoopy-> referer = "https://wx.qq.com /";
  127. $ Send_snoopy-> fetch ($ url );
  128. $ Result = $ send_snoopy-> results;
  129. $ This-> log ('step1: '. $ result );
  130. If ($ result ){
  131. Preg_match ("/window \. code = (\ d +)/", $ result, $ matches );
  132. If (count ($ matches)> 1 ){
  133. $ Status = intval ($ matches [1]);
  134. If ($ status = 201) $ _ SESSION ['login _ step'] = 1;
  135. If ($ status = 200 ){
  136. Preg_match ("/ticket = ([0-9a-z-_] +) & lang = zh_CN & scan = (\ d +)/", $ result, $ matches );
  137. $ This-> log ('step2: '. print_r ($ matches, true ));
  138. If (count ($ matches)> 1 ){
  139. $ Ticket = $ matches [1];
  140. $ Scan = $ matches [2];
  141. $ Loginurl = 'https: // wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage? Ticket = '. $ ticket.' & lang = zh_CN & scan = '. $ scan.' & fun = new ';
  142. $ Send_snoopy = new Snoopy;
  143. $ Send_snoopy-> referer = "https://wx.qq.com /";
  144. $ Send_snoopy-> fetch ($ loginurl );
  145. $ This-> log ('step3: '. print_r ($ send_snoopy-> headers, true ));
  146. Foreach ($ send_snoopy-> headers as $ key => $ value ){
  147. $ Value = trim ($ value );
  148. If (strpos ($ value, 'set-Cookie :')! = False ){
  149. $ Tmp = str_replace ("Set-Cookie:", "", $ value );
  150. $ Tmp = str_replace ("Path =/", "", $ tmp );
  151. $ Tmp = str_replace ("Domain = .qq.com;", "", $ tmp );
  152. $ Cookie. = $ tmp;
  153. }
  154. }
  155. $ Cookie. = "Domain = .qq.com ;";
  156. $ This-> cookie = $ cookie;
  157. $ This-> saveCookie ($ this-> _ cookiename, $ this-> cookie );
  158. }
  159. }
  160. Return $ status;
  161. }
  162. }
  163. Return false;
  164. }
  165. /**
  166. * Obtain the logon cookie.
  167. *
  168. * @ Param bool $ is_array: whether to return a value. The default value is no. the returned string is
  169. * @ Return string | array
  170. */
  171. Public function get_login_cookie ($ is_array = false ){
  172. If (! $ Is_array) return $ this-> cookie;
  173. $ C_arr = explode (';', $ this-> cookie );
  174. $ Cookie = array ();
  175. Foreach ($ c_arr as $ item ){
  176. $ Kitem = explode ('=', trim ($ item ));
  177. If (count ($ kitem)> 1 ){
  178. $ Key = trim ($ kitem [0]);
  179. $ Val = trim ($ kitem [1]);
  180. If (! Empty ($ val) $ cookie [$ key] = $ val;
  181. }
  182. }
  183. Return $ cookie;
  184. }
  185. /**
  186. * Obtain user login information after authorized login
  187. */
  188. Public function get_login_info (){
  189. If (! $ This-> cookie) return false;
  190. $ T = time (). strval (mt_rand (100,999 ));
  191. $ Send_snoopy = new Snoopy;
  192. $ Submit = 'https: // wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit? R = '. $ t;
  193. $ Send_snoopy-> rawheaders ['cookies'] = $ this-> Cookie;
  194. $ Send_snoopy-> referer = "https://wx.qq.com /";
  195. $ Send_snoopy-> submit ($ submit, array ());
  196. $ This-> log ('login _ info: '. $ send_snoopy-> results );
  197. $ Result = json_decode ($ send_snoopy-> results, true );
  198. If ($ result ['baseresponse'] ['ret '] <0) return false;
  199. $ This-> _ login_user = $ result ['user'];
  200. Return $ result;
  201. }
  202. /**
  203. * Get the Avatar
  204. * @ Param string $ url refers to the Avatar address obtained from the user information interface.
  205. */
  206. Public function get_avatar ($ url ){
  207. If (! $ This-> cookie) return false;
  208. If (strpos ($ url, 'http ') = false ){
  209. $ Url = 'http: // wx.qq.com '. $ url;
  210. }
  211. $ Send_snoopy = new Snoopy;
  212. $ Send_snoopy-> rawheaders ['cookies'] = $ this-> Cookie;
  213. $ Send_snoopy-> referer = "https://wx.qq.com /";
  214. $ Send_snoopy-> fetch ($ url );
  215. $ Result = $ send_snoopy-> results;
  216. If ($ result)
  217. Return $ result;
  218. Else
  219. Return false;
  220. }
  221. /**
  222. * Log out of the current login user
  223. */
  224. Public function logout (){
  225. If (! $ This-> cookie) return false;
  226. Preg_match ("/wxuin = (\ w +);/", $ this-> cookie, $ matches );
  227. If (count ($ matches)> 1) $ uid = $ matches [1];
  228. Preg_match ("/wxsid = (\ w +);/", $ this-> cookie, $ matches );
  229. If (count ($ matches)> 1) $ sid = $ matches [1];
  230. $ This-> log ('logout: uid = '. $ uid.'; sid = '. $ sid );
  231. $ Send_snoopy = new Snoopy;
  232. $ Submit = 'https: // wx.qq.com/cgi-bin/mmwebwx-bin/webwxlogout? Redirect = 1 & type = 1 ';
  233. $ Send_snoopy-> rawheaders ['cookies'] = $ this-> Cookie;
  234. $ Send_snoopy-> referer = "https://wx.qq.com /";
  235. $ Send_snoopy-> submit ($ submit, array ('uin' => $ uid, 'Sid '=> $ sid ));
  236. $ This-> deleteCookie ($ this-> _ cookiename );
  237. Return true;
  238. }
  239. }

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.