首頁  >  文章  >  後端開發  >  微信掃描登入

微信掃描登入

WBOY
WBOY原創
2016-07-25 08:49:061251瀏覽
}}
使用者透過掃描網頁提供的二維碼實現登陸資訊獲取
  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. {
  21. private $cookie;
  22. private $_cookiename;
  23. private $_cookieexpired = 3600;
  24. private $_account = 'test';
  25. private $_data = 'private $_account = 'test';
  26. private $_data = './/data/ookie^'ate; $debug;
  27. private $_logcallback;
  28. public $login_user; //目前登陸使用者, 呼叫get_login_info後取得
  29. public function __construct($options)
  30. {🎜> public function __construct($options)
  31. { $_account = isset($options['account'])?$options['account']:'';
  32. $this->_datapath = isset($options['datapath'])?$options['datapath ']:$this->_datapath;
  33. $this->debug = isset($options['debug'])?$options['debug']:false;
  34. $this->_logcallback = isset( $options['logcallback'])?$options['logcallback']:false;
  35. $this->_cookiename = $this->_datapath.$this->_account;
  36. $this->getCookie($ this->_cookiename);
  37. }
  38. /**
  39. * 把cookie寫入快取
  40. * @param string $filename 快取檔案名稱
  41. * @param string $content 檔案內容
  42. * @return bool
  43. */
  44. public function saveCookie($filename,$content){
  45. return file_put_contents($filename,$content);
  46. return file_put_contents($filename,$content);
  47. }
  48. /**
  49. * 讀取cookie快取內容
  50. * @param string $filename 快取檔案名稱
  51. * @return string cookie
  52. */
  53. public function getCookie($filename){
  54. if (file_exists($filename)) {
  55. $mtime = filemtime($filename) ;
  56. if ($mtime
  57. $data = file_get_contents($filename);
  58. if ($data) $this->cookie = $data ;
  59. }
  60. return $this->cookie;
  61. }
  62. /*
  63. * 刪除cookie
  64. */
  65. public function deleteCookie($filename) {
  66. $this->cookie = '';
  67. @unlink($filename);
  68. return true;
  69. }
  70. private function log($log){
  71. if ( $this->debug && function_exists($this->_logcallback)) {
  72. if (is_array($log)) $log = print_r($log,true);
  73. return call_user_func($this->_logcallback, $log);
  74. }
  75. }
  76. /**
  77. * 取得登陸二維碼對應的授權碼
  78. */
  79. public function get_login_code(){
  80. if ($this->_logincode) return $this-this- >_logincode;
  81. $t = time().strval(mt_rand(100,999));
  82. $codeurl = 'https://login.weixin.mt_rand(100,999));
  83. $codeurl = 'https://login.weixin.mt_rand(100,999));
  84. $codeurl = 'https://login.weixin.cffqq.com/jslogin?appid=wx782c26e4c19acffqq.com/jslogin?appid=wx782c26e4c19acffq wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage&fun=new&lang=zh_CN&_='.$t;
  85. $send_snoopy = new Snoopy;
  86. $send_snoopy->fetch($codeurl); $result = $send_snoopy->results;
  87. if ($result) {
  88. preg_match("/window.QRLogin.uuids =s "([^"] )"/",$result,$matches);
  89. if(count($matches)>1) {
  90. $this->_logincode = $matches[1];
  91. $_SESSION['login_step'] = 0;
  92. return $this-> _logincode;
  93. } } return $result; }
  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. *
  117. * @return status:
  118. * >=400:二維碼無效;408:未經授權並等待,400,401:無效或過期
  119. * 201:剛掃描但未確認
  120. * 200:確認即可取得使用者資訊
  121. */
  122. public function verify_code() {
  123. if (!$this->_logincode) return false;
  124. $t = time().strval (mt_rand( 100,999));
  125. $url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid='.$this->_logincode. '&tip=1&_='.$t;
  126. $send_snoopy = 新史努比;
  127. $send_snoopy->referer = "https://wx.qq.com/";
  128. $send_snoopy->fetch($url);
  129. $result = $send_snoopy->results;
  130. $this->log('step1:'.$result);
  131. if ($result) {
  132. preg_match("/window.code=(d )/",$result,$matches ) ;
  133. if(count($matches)>1) {
  134. $status = intval($matches[1]);
  135. if ($status==201) $_SESSION['login_step'] = 1 ;
  136. if ($status==200) {
  137. preg_match("/ticket=([0-9a-z-_] )&lang=zh_CN&scan=(d )/",$結果,$matches );
  138. $this->log('step2:'.print_r($matches,true));
  139. if (count($matches)>1) {
  140. $ticket = $matches[ 1];
  141. $scan = $matches[2];
  142. $loginurl = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket='.$ticket.' &lang=zh_CN&scan='.$scan.'&fun=new';
  143. $send_snoopy = 新史努比;
  144. $send_snoopy->referer = "https://wx.qq.com/";
  145. $send_snoopy->fetch($loginurl);
  146. $this->log('step3: '.print_r ($send_snoopy->headers,true));
  147. foreach ($send_snoopy->headers as $key => $value) {
  148. $value = trim($value);
  149. if(strpos($ ) value,'Set-Cookie: ') !== false){
  150. $tmp = str_replace("Set-Cookie: ","",$value);
  151. $tmp = str_replace ("Path=/ " ,"",$tmp);
  152. $tmp = str_replace("網域=.qq.com; ","",$tmp);
  153. $cookie.=$tmp;
  154. }
  155. }
  156. $cookie .="Domain=.qq.com;";
  157. $this->cookie = $cookie;
  158. $this->saveCookie($this->_cookiename,$this-> Cookie );
  159. }
  160. }
  161. 回傳 $status;
  162. }
  163. }
  164. 回傳 false; }
  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 =arrexplode (';',$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 = 新史努比;
  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']
  199. if ($result['BaseResponse'] ['Ret']
  200. if ($result['BaseResponse'] ['Ret']
  201. if ($result['BaseResponse'] ['Ret'] $this->_login_user = $result['User'];
  202. return $result;
  203. }
  204. /**
  205. * 取得頭像
  206. * @param string $url 傳入從使用者資訊介面取得到的頭像位址
  207. */
  208. public function get_avatar($url) {
  209. if (!$this->cookie) return false;
  210. if (strpos($url, 'http')===false ) {
  211. $url = 'http ://wx.qq.com'.$url;
  212. }
  213. $send_snoopy = 新史努比;
  214. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  215. $send_snoopy->referer = "https://wx.qq.com/";
  216. $send_snoopy ->fetch ($url);
  217. $result = $send_snoopy->results;
  218. if ($result)
  219. return $result;
  220. else
  221. return false;
  222. }
  223. return false;
  224. }
  225. /**
  226. * 登出目前登陸用戶
  227. */
  228. public function logout(){
  229. if (!$this->cookie) return false;
  230. preg_match("/wxuin=( w );/", $this->cookie,$matches);
  231. if (count($matches)>1) $uid = $matches[1];
  232. preg_match("/wxsid=( w );/",$this ->cookie,$matches);
  233. if (count($matches)>1) $sid = $matches[1];
  234. $this->log( '註銷:uid='.$uid.' ;sid='.$sid);
  235. $send_snoopy = 新史努比;
  236. $submit = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxlogout?redirect=1&type=1';
$send_snoopy->rawheaders['Cookie']= $ this ->cookie;
$send_snoopy->referer = "https://wx.qq.com/"; $send_snoopy->submit($submit,array('uin'=>; $uid,' sid'=>$sid)); $this->deleteCookie($this->_cookiename);
return true;
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn