微信掃描登入

來源:互聯網
上載者:User
使用者通過掃描網頁提供的二維碼實現登陸資訊擷取
  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_cookieexpired) return false;
  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']<0) return false;
  199. $this->_login_user = $result['User'];
  200. return $result;
  201. }
  202. /**
  203. * 擷取頭像
  204. * @param string $url 傳入從使用者資訊介面擷取到的頭像地址
  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. * 登出當前登陸使用者
  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. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.