wechatauth.class.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_cookie() 鉴定成功后调用此方法即可获取用户基本信息
  11. * sendNews($account,$title,$summary,$content,$pic,$srcurl='') 向一个微信账户发送图文信息
  12. * get_avatar($url) 获取用户头像图片数据
  13. * @author dodge <dodgepudding@gmail.com>
  14. * @link https://github.com/dodgepudding/wechat-php-sdk
  15. * @version 1.1
  16. *
  17. */
  18. include "snoopy.class.php";
  19. class Wechatauth
  20. {
  21. private $cookie;
  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 file_put_contents($filename,$content);
  46. }
  47. /**
  48. * 读取cookie缓存内容
  49. * @param string $filename 缓存文件名
  50. * @return string cookie
  51. */
  52. public function getCookie($filename){
  53. if (file_exists($filename)) {
  54. $mtime = filemtime($filename);
  55. if ($mtime<time()-$this->_cookieexpired) return false;
  56. $data = file_get_contents($filename);
  57. if ($data) $this->cookie = $data;
  58. }
  59. return $this->cookie;
  60. }
  61. /*
  62. * 删除cookie
  63. */
  64. public function deleteCookie($filename) {
  65. $this->cookie = '';
  66. @unlink($filename);
  67. return true;
  68. }
  69. private function log($log){
  70. if ($this->debug && function_exists($this->_logcallback)) {
  71. if (is_array($log)) $log = print_r($log,true);
  72. return call_user_func($this->_logcallback,$log);
  73. }
  74. }
  75. /**
  76. * 获取登陆二维码对应的授权码
  77. */
  78. public function get_login_code(){
  79. if ($this->_logincode) return $this->_logincode;
  80. $t = time().strval(mt_rand(100,999));
  81. $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;
  82. $send_snoopy = new Snoopy;
  83. $send_snoopy->fetch($codeurl);
  84. $result = $send_snoopy->results;
  85. if ($result) {
  86. preg_match("/window.QRLogin.uuid\s+=\s+\"([^\"]+)\"/",$result,$matches);
  87. if(count($matches)>1) {
  88. $this->_logincode = $matches[1];
  89. $_SESSION['login_step'] = 0;
  90. return $this->_logincode;
  91. }
  92. }
  93. return $result;
  94. }
  95. /**
  96. * 通过授权码获取对应的二维码图片地址
  97. * @param string $code
  98. * @return string image url
  99. */
  100. public function get_code_image($code=''){
  101. if ($code=='') $code = $this->_logincode;
  102. if (!$code) return false;
  103. return 'http://login.weixin.qq.com/qrcode/'.$this->_logincode.'?t=webwx';
  104. }
  105. /**
  106. * 设置二维码对应的授权码
  107. * @param string $code
  108. * @return class $this
  109. */
  110. public function set_login_code($code) {
  111. $this->_logincode = $code;
  112. return $this;
  113. }
  114. /**
  115. * 二维码登陆验证
  116. *
  117. * @return status:
  118. * >=400: invaild code; 408: not auth and wait, 400,401: not valid or expired
  119. * 201: just scaned but not confirm
  120. * 200: confirm then you can get user info
  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 = new 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+)/",$result,$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 = new 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("Domain=.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. return $status;
  162. }
  163. }
  164. return false;
  165. }
  166. /**
  167. * 获取登陆的cookie
  168. *
  169. * @param bool $is_array 是否以数值方式返回,默认否,返回字符串
  170. * @return string|array
  171. */
  172. public function get_login_cookie($is_array = false){
  173. if (!$is_array) return $this->cookie;
  174. $c_arr = explode(';',$this->cookie);
  175. $cookie = array();
  176. foreach($c_arr as $item) {
  177. $kitem = explode('=',trim($item));
  178. if (count($kitem)>1) {
  179. $key = trim($kitem[0]);
  180. $val = trim($kitem[1]);
  181. if (!empty($val)) $cookie[$key] = $val;
  182. }
  183. }
  184. return $cookie;
  185. }
  186. /**
  187. * 授权登陆后获取用户登陆信息
  188. */
  189. public function get_login_info(){
  190. if (!$this->cookie) return false;
  191. $t = time().strval(mt_rand(100,999));
  192. $send_snoopy = new Snoopy;
  193. $submit = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r='.$t;
  194. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  195. $send_snoopy->referer = "https://wx.qq.com/";
  196. $send_snoopy->submit($submit,array());
  197. $this->log('login_info:'.$send_snoopy->results);
  198. $result = json_decode($send_snoopy->results,true);
  199. if ($result['BaseResponse']['Ret']<0) return false;
  200. $this->_login_user = $result['User'];
  201. return $result;
  202. }
  203. /**
  204. * 获取头像
  205. * @param string $url 传入从用户信息接口获取到的头像地址
  206. */
  207. public function get_avatar($url) {
  208. if (!$this->cookie) return false;
  209. if (strpos($url, 'http')===false) {
  210. $url = 'http://wx.qq.com'.$url;
  211. }
  212. $send_snoopy = new Snoopy;
  213. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  214. $send_snoopy->referer = "https://wx.qq.com/";
  215. $send_snoopy->fetch($url);
  216. $result = $send_snoopy->results;
  217. if ($result)
  218. return $result;
  219. else
  220. return false;
  221. }
  222. /**
  223. * 登出当前登陆用户
  224. */
  225. public function logout(){
  226. if (!$this->cookie) return false;
  227. preg_match("/wxuin=(\w+);/",$this->cookie,$matches);
  228. if (count($matches)>1) $uid = $matches[1];
  229. preg_match("/wxsid=(\w+);/",$this->cookie,$matches);
  230. if (count($matches)>1) $sid = $matches[1];
  231. $this->log('logout: uid='.$uid.';sid='.$sid);
  232. $send_snoopy = new Snoopy;
  233. $submit = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxlogout?redirect=1&type=1';
  234. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  235. $send_snoopy->referer = "https://wx.qq.com/";
  236. $send_snoopy->submit($submit,array('uin'=>$uid,'sid'=>$sid));
  237. $this->deleteCookie($this->_cookiename);
  238. return true;
  239. }
  240. }