Wechatauth.class.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * 微信公众平台PHP-SDK
  4. * Wechatauth为非官方微信登陆API
  5. * 用户通过扫描网页提供的二维码实现登陆信息获取
  6. * 主要实现如下功能:
  7. * get_login_code() 获取登陆授权码, 通过授权码才能获取二维码
  8. * get_code_image($code='') 将上面获取的授权码转换为图片二维码
  9. * verify_code() 鉴定是否登陆成功,返回200为最终授权成功.
  10. * get_login_info() 鉴定成功后调用此方法即可获取用户基本信息
  11. * get_avatar($url) 获取用户头像图片数据
  12. * @author dodge <dodgepudding@gmail.com>
  13. * @link 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 $skey;
  22. private $_cookiename;
  23. private $_cookieexpired = 3600;
  24. private $_account = 'test';
  25. private $_datapath = './data/cookie_';
  26. private $debug;
  27. private $_logcallback;
  28. public $login_user; //当前登陆用户, 调用get_login_info后获取
  29. public function __construct($options)
  30. {
  31. $this->_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 S($filename,$content,$this->_cookieexpired);
  46. }
  47. /**
  48. * 读取cookie缓存内容
  49. * @param string $filename 缓存文件名
  50. * @return string cookie
  51. */
  52. public function getCookie($filename){
  53. $data = S($filename);
  54. if ($data) $this->cookie = $data;
  55. return $this->cookie;
  56. }
  57. /*
  58. * 删除cookie
  59. */
  60. public function deleteCookie($filename) {
  61. $this->cookie = '';
  62. S($filename,null);
  63. return true;
  64. }
  65. private function log($log){
  66. if ($this->debug && function_exists($this->_logcallback)) {
  67. if (is_array($log)) $log = print_r($log,true);
  68. return call_user_func($this->_logcallback,$log);
  69. }
  70. }
  71. /**
  72. * 获取登陆二维码对应的授权码
  73. */
  74. public function get_login_code(){
  75. if ($this->_logincode) return $this->_logincode;
  76. $t = time().strval(mt_rand(100,999));
  77. $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;
  78. $send_snoopy = new Snoopy;
  79. $send_snoopy->fetch($codeurl);
  80. $result = $send_snoopy->results;
  81. if ($result) {
  82. preg_match("/window.QRLogin.uuid\s+=\s+\"([^\"]+)\"/",$result,$matches);
  83. if(count($matches)>1) {
  84. $this->_logincode = $matches[1];
  85. $_SESSION['login_step'] = 0;
  86. return $this->_logincode;
  87. }
  88. }
  89. return $result;
  90. }
  91. /**
  92. * 通过授权码获取对应的二维码图片地址
  93. * @param string $code
  94. * @return string image url
  95. */
  96. public function get_code_image($code=''){
  97. if ($code=='') $code = $this->_logincode;
  98. if (!$code) return false;
  99. return 'http://login.weixin.qq.com/qrcode/'.$this->_logincode.'?t=webwx';
  100. }
  101. /**
  102. * 设置二维码对应的授权码
  103. * @param string $code
  104. * @return class $this
  105. */
  106. public function set_login_code($code) {
  107. $this->_logincode = $code;
  108. return $this;
  109. }
  110. /**
  111. * 二维码登陆验证
  112. *
  113. * @return status:
  114. * >=400: invaild code; 408: not auth and wait, 400,401: not valid or expired
  115. * 201: just scaned but not confirm
  116. * 200: confirm then you can get user info
  117. */
  118. public function verify_code() {
  119. if (!$this->_logincode) return false;
  120. $t = time().strval(mt_rand(100,999));
  121. $url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid='.$this->_logincode.'&tip=0&_='.$t;
  122. $send_snoopy = new Snoopy;
  123. $send_snoopy->referer = "https://wx.qq.com/";
  124. $send_snoopy->fetch($url);
  125. $result = $send_snoopy->results;
  126. $this->log('step1:'.$result);
  127. if ($result) {
  128. preg_match("/window\.code=(\d+)/",$result,$matches);
  129. if(count($matches)>1) {
  130. $status = intval($matches[1]);
  131. if ($status==201) $_SESSION['login_step'] = 1;
  132. if ($status==200) {
  133. preg_match("/ticket=([0-9a-z-_]+)&lang=zh_CN&scan=(\d+)/",$result,$matches);
  134. preg_match("/window.redirect_uri=\"([^\"]+)\"/",$result,$matcheurl);
  135. $this->log('step2:'.print_r($matches,true));
  136. if (count($matcheurl)>1) {
  137. $ticket = $matches[1];
  138. $scan = $matches[2];
  139. //$loginurl = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket='.$ticket.'&lang=zh_CN&scan='.$scan.'&fun=new';
  140. $loginurl = str_replace("wx.qq.com", "wx2.qq.com", $matcheurl[1]).'&fun=old';
  141. $urlpart = parse_url($loginurl);
  142. $send_snoopy = new Snoopy;
  143. $send_snoopy->referer = "https://{$urlpart['host']}/cgi-bin/mmwebwx-bin/webwxindex?t=chat";
  144. $send_snoopy->fetch($loginurl);
  145. $result = $send_snoopy->results;
  146. $xml = simplexml_load_string($result);
  147. if ($xml->ret=="0") $this->skey = $xml->skey;
  148. foreach ($send_snoopy->headers as $key => $value) {
  149. $value = trim($value);
  150. if(strpos($value,'Set-Cookie: ') !== false){
  151. $tmp = str_replace("Set-Cookie: ","",$value);
  152. $tmparray = explode(';', $tmp);
  153. $item = trim($tmparray[0]);
  154. $cookie.=$item.';';
  155. }
  156. }
  157. $cookie .="Domain=.qq.com;";
  158. $this->cookie = $cookie;
  159. $this->log('step3:'.$loginurl.';cookie:'.$cookie.';respond:'.$result);
  160. $this->saveCookie($this->_cookiename,$this->cookie);
  161. }
  162. }
  163. return $status;
  164. }
  165. }
  166. return false;
  167. }
  168. /**
  169. * 获取登陆的cookie
  170. *
  171. * @param bool $is_array 是否以数值方式返回,默认否,返回字符串
  172. * @return string|array
  173. */
  174. public function get_login_cookie($is_array = false){
  175. if (!$is_array) return $this->cookie;
  176. $c_arr = explode(';',$this->cookie);
  177. $cookie = array();
  178. foreach($c_arr as $item) {
  179. $kitem = explode('=',trim($item));
  180. if (count($kitem)>1) {
  181. $key = trim($kitem[0]);
  182. $val = trim($kitem[1]);
  183. if (!empty($val)) $cookie[$key] = $val;
  184. }
  185. }
  186. return $cookie;
  187. }
  188. /**
  189. * 授权登陆后获取用户登陆信息
  190. */
  191. public function get_login_info(){
  192. if (!$this->cookie) return false;
  193. $t = time().strval(mt_rand(100,999));
  194. $send_snoopy = new Snoopy;
  195. $submit = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r='.$t.'&skey='.urlencode($this->skey);
  196. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  197. $send_snoopy->referer = "https://wx2.qq.com/";
  198. $citems = $this->get_login_cookie(true);
  199. $post = array(
  200. "BaseRequest"=>array(
  201. array(
  202. "Uin"=>$citems['wxuin'],
  203. "Sid"=>$citems['wxsid'],
  204. "Skey"=>$this->skey,
  205. "DeviceID"=>''
  206. )
  207. )
  208. );
  209. $send_snoopy->submit($submit,json_encode($post));
  210. $this->log('login_info:'.$send_snoopy->results);
  211. $result = json_decode($send_snoopy->results,true);
  212. if ($result['BaseResponse']['Ret']<0) return false;
  213. $this->_login_user = $result['User'];
  214. return $result;
  215. }
  216. /**
  217. * 获取头像
  218. * @param string $url 传入从用户信息接口获取到的头像地址
  219. */
  220. public function get_avatar($url) {
  221. if (!$this->cookie) return false;
  222. if (strpos($url, 'http')===false) {
  223. $url = 'http://wx2.qq.com'.$url;
  224. }
  225. $send_snoopy = new Snoopy;
  226. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  227. $send_snoopy->referer = "https://wx2.qq.com/";
  228. $send_snoopy->fetch($url);
  229. $result = $send_snoopy->results;
  230. if ($result)
  231. return $result;
  232. else
  233. return false;
  234. }
  235. /**
  236. * 登出当前登陆用户
  237. */
  238. public function logout(){
  239. if (!$this->cookie) return false;
  240. preg_match("/wxuin=(\w+);/",$this->cookie,$matches);
  241. if (count($matches)>1) $uid = $matches[1];
  242. preg_match("/wxsid=(\w+);/",$this->cookie,$matches);
  243. if (count($matches)>1) $sid = $matches[1];
  244. $this->log('logout: uid='.$uid.';sid='.$sid);
  245. $send_snoopy = new Snoopy;
  246. $submit = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxlogout?redirect=1&type=1';
  247. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  248. $send_snoopy->referer = "https://wx2.qq.com/";
  249. $send_snoopy->submit($submit,array('uin'=>$uid,'sid'=>$sid));
  250. $this->deleteCookie($this->_cookiename);
  251. return true;
  252. }
  253. }