WeChat Scan Login

Source: Internet
Author: User
The user obtains the login information by scanning the QR code provided by the webpage.
  1. /**
  2. * Public Platform PHP-SDK
  3. * Wechatauth for unofficial Login API
  4. * Users to access the QR code provided by scanning the Web page to achieve the landing information
  5. * The main implementation of the following functions:
  6. * Get_login_code () to obtain a login authorization code, through the authorization code to obtain the QR code
  7. * Get_code_image ($code = ") Converts the authorization code obtained above to the image QR code
  8. * Verify_code () to determine if the login was successful, return 200 for final authorization success.
  9. * Get_login_cookie () Call this method to obtain basic user information after successful authentication
  10. * Sendnews ($account, $title, $summary, $content, $pic, $srcurl = ") send text messages to an account
  11. * Get_avatar ($url) Get 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; Current login user, call Get_login_info after get
  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 cookies to cache
  39. * @param string $filename cache file name
  40. * @param string $content file contents
  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 A 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 Cookies
  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. * Get Authorization code for 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=wx782c26e4c19acffb&redirect_uri=https%3A%2F% 2fwx.qq.com%2fcgi-bin%2fmmwebwx-bin%2fwebwxnewloginpage&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 by 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/'. $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. * Access to login cookies
  167. *
  168. * @param bool $is Whether _array is returned numerically, default no, returns a string
  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. * Access to 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[' Cookie ']= $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 Avatar
  204. * @param string $url Pass in 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[' Cookie ']= $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[' Cookie ']= $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. }
Copy Code
  • 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.