Home > Backend Development > PHP Tutorial > WeChat scan login

WeChat scan login

WBOY
Release: 2016-07-25 08:49:06
Original
1284 people have browsed it
Users can obtain login information by scanning the QR code provided on the web page.
  1. /**
  2. * WeChat Public Platform PHP-SDK
  3. * Wechatauth is the unofficial WeChat login API
  4. * Users can obtain login information by scanning the QR code provided on the webpage
  5. * Mainly implements the following functions:
  6. * get_login_code() to obtain the login authorization code, through Only the authorization code can be obtained to obtain the QR code
  7. * get_code_image($code='') Convert the authorization code obtained above into the image QR code
  8. * verify_code() Verify whether the login is successful and return 200 for final authorization success.
  9. * get_login_cookie( ) After successful authentication, call this method to obtain the user's basic information
  10. * sendNews($account,$title,$summary,$content,$pic,$srcurl='') Send graphic and text information to a WeChat account
  11. * get_avatar( $url) Get user avatar image 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 logged in user, get it after calling get_login_info
  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 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. * Get 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=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.uuids+=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 through 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/'.$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. * 二维码登陆验证
  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. * Get the login cookie
  167. *
  168. * @param bool $is_array Whether to return in numerical form, the default is no, return 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. * 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['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 the currently logged in 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. }
复制代码


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template