Heim >Backend-Entwicklung >PHP-Tutorial >微信扫描登录

微信扫描登录

WBOY
WBOYOriginal
2016-07-25 08:49:061278Durchsuche
用户通过扫描网页提供的二维码实现登陆信息获取
  1. /**
  2. * 微信公众平台PHP-SDK
  3. * Wechatauth为非官方微信登陆API
  4. * 用户通过扫描网页提供的二维码实现登陆信息获取
  5. * 主要实现如下功能:
  6. * get_login_code() 获取登陆授权码, 通过授权码才能获取二维码
  7. * get_code_image($code='') 将上面获取的授权码转换为图片二维码
  8. * verify_code() 鉴定是否登陆成功,返回200为最终授权成功.
  9. * get_login_cookie() 鉴定成功后调用此方法即可获取用户基本信息
  10. * sendNews($account,$title,$summary,$content,$pic,$srcurl='') 向一个微信账户发送图文信息
  11. * get_avatar($url) 获取用户头像图片数据
  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; //当前登陆用户, 调用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. * 把cookie写入缓存
  39. * @param string $filename 缓存文件名
  40. * @param string $content 文件内容
  41. * @return bool
  42. */
  43. public function saveCookie($filename,$content){
  44. return file_put_contents($filename,$content);
  45. }
  46. /**
  47. * 读取cookie缓存内容
  48. * @param string $filename 缓存文件名
  49. * @return string cookie
  50. */
  51. public function getCookie($filename){
  52. if (file_exists($filename)) {
  53. $mtime = filemtime($filename);
  54. if ($mtime
  55. $data = file_get_contents($filename);
  56. if ($data) $this->cookie = $data;
  57. }
  58. return $this->cookie;
  59. }
  60. /*
  61. * 删除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. * 获取登陆二维码对应的授权码
  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. * 通过授权码获取对应的二维码图片地址
  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. * 设置二维码对应的授权码
  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. * 获取登陆的cookie
  167. *
  168. * @param bool $is_array 是否以数值方式返回,默认否,返回字符串
  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. * 授权登陆后获取用户登陆信息
  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'] $this->_login_user = $result['User'];
  199. return $result;
  200. }
  201. /**
  202. * 获取头像
  203. * @param string $url 传入从用户信息接口获取到的头像地址
  204. */
  205. public function get_avatar($url) {
  206. if (!$this->cookie) return false;
  207. if (strpos($url, 'http')===false) {
  208. $url = 'http://wx.qq.com'.$url;
  209. }
  210. $send_snoopy = new Snoopy;
  211. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  212. $send_snoopy->referer = "https://wx.qq.com/";
  213. $send_snoopy->fetch($url);
  214. $result = $send_snoopy->results;
  215. if ($result)
  216. return $result;
  217. else
  218. return false;
  219. }
  220. /**
  221. * 登出当前登陆用户
  222. */
  223. public function logout(){
  224. if (!$this->cookie) return false;
  225. preg_match("/wxuin=(\w+);/",$this->cookie,$matches);
  226. if (count($matches)>1) $uid = $matches[1];
  227. preg_match("/wxsid=(\w+);/",$this->cookie,$matches);
  228. if (count($matches)>1) $sid = $matches[1];
  229. $this->log('logout: uid='.$uid.';sid='.$sid);
  230. $send_snoopy = new Snoopy;
  231. $submit = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxlogout?redirect=1&type=1';
  232. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  233. $send_snoopy->referer = "https://wx.qq.com/";
  234. $send_snoopy->submit($submit,array('uin'=>$uid,'sid'=>$sid));
  235. $this->deleteCookie($this->_cookiename);
  236. return true;
  237. }
  238. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn