Users can obtain login information by scanning the QR code provided by the webpage. For more information, see.
Users can obtain login information by scanning the QR code provided by the webpage. For more information, see.
Download the snoopy class first
The Code is as follows:
/**
* Public platform PHP-SDK
* Wechatauth is an unofficial API Login
* Users can obtain login information by scanning the QR code provided by the webpage.
* Mainly implements the following functions:
* Get_login_code () obtains the logon authorization code. Only the two-dimensional code can be obtained through the authorization code.
* Get_code_image ($ code = '') converts the authorization code obtained above to the image QR code
* Verify_code () indicates whether the logon is successful. If 200 is returned, the final authorization is successful.
* After the get_login_cookie () authentication is successful, call this method to obtain basic user information.
* SendNews ($ account, $ title, $ summary, $ content, $ pic, $ srcurl = '') sends graphic information to an account
* Get_avatar ($ url) obtains the user's profile picture data
* @ Author dodge
* @ Link https://github.com/dodgepudding/wechat-php-sdk
* @ Version 1.1
*
*/
Include "snoopy. class. php ";
Class Wechatauth
{
Private $ cookie;
Private $ _ cookiename;
Private $ _ cookieexpired = 3600;
Private $ _ account = 'test ';
Private $ _ datapath = './data/cookie _';
Private $ debug;
Private $ _ logcallback;
Public $ login_user; // The current login user, which is obtained after get_login_info is called.
Public function _ construct ($ options)
{
$ This-> _ account = isset ($ options ['account'])? $ Options ['account']: '';
$ This-> _ datapath = isset ($ options ['datapath'])? $ Options ['datapath']: $ this-> _ datapath;
$ This-> debug = isset ($ options ['debug'])? $ Options ['debug']: false;
$ This-> _ logcallback = isset ($ options ['logcallback'])? $ Options ['logcallback']: false;
$ This-> _ cookiename = $ this-> _ datapath. $ this-> _ account;
$ This-> getCookie ($ this-> _ cookiename );
}
/**
* Write the cookie to the cache.
* @ Param string $ filename cache file name
* @ Param string $ content File content
* @ Return bool
*/
Public function saveCookie ($ filename, $ content ){
Return file_put_contents ($ filename, $ content );
}
/**
* Read cookie cache content
* @ Param string $ filename cache file name
* @ Return string cookie
*/
Public function getCookie ($ filename ){
If (file_exists ($ filename )){
$ Mtime = filemtime ($ filename );
If ($ mtime _ Cookieexpired) return false;
$ Data = file_get_contents ($ filename );
If ($ data) $ this-> cookie = $ data;
}
Return $ this-> cookie;
}
/*
* Delete a cookie
*/
Public function deleteCookie ($ filename ){
$ This-> cookie = '';
@ Unlink ($ filename );
Return true;
}
Private function log ($ log ){
If ($ this-> debug & function_exists ($ this-> _ logcallback )){
If (is_array ($ log) $ log = print_r ($ log, true );
Return call_user_func ($ this-> _ logcallback, $ log );
}
}
/**
* Obtain the authorization code corresponding to the login QR code
*/
Public function get_login_code (){
If ($ this-> _ logincode) return $ this-> _ logincode;
$ T = time (). strval (mt_rand (100,999 ));
$ Codeurl = 'https: // login.weixin.qq.com/jslogin? Appid = signature & redirect_uri = https % 3A % 2F % 2Fwx.qq.com % 2Fcgi-bin % 2Fmmwebwx-bin % 2 Fwebwxnewloginpage & fun = new & lang = zh_CN & _ = '. $ t;
$ Send_snoopy = new Snoopy;
$ Send_snoopy-> fetch ($ codeurl );
$ Result = $ send_snoopy-> results;
If ($ result ){
Preg_match ("/window. QRLogin. uuid \ s + = \ s + \" ([^ \ "] +) \"/", $ result, $ matches );
If (count ($ matches)> 1 ){
$ This-> _ logincode = $ matches [1];
$ _ SESSION ['login _ step'] = 0;
Return $ this-> _ logincode;
}
}
Return $ result;
}
/**
* Obtain the corresponding QR code image address using the authorization code
* @ Param string $ code
* @ Return string image url
*/
Public function get_code_image ($ code = ''){
If ($ code = '') $ code = $ this-> _ logincode;
If (! $ Code) return false;
Return 'HTTP: // login.weixin.qq.com/qrcode/'.w.this-> _ logincode .'? T = webwx ';
}
/**
* Set the authorization code corresponding to the QR code
* @ Param string $ code
* @ Return class $ this
*/
Public function set_login_code ($ code ){
$ This-> _ logincode = $ code;
Return $ this;
}
/**
* QR code login verification
*
* @ Return status:
*> = 400: invaild code; 408: not auth and wait, 400,401: not valid or expired
* 201: just scaned but not confirm
* 200: confirm then you can get user info
*/
Public function verify_code (){
If (! $ This-> _ logincode) return false;
$ T = time (). strval (mt_rand (100,999 ));
$ Url = 'https: // login.weixin.qq.com/cgi-bin/mmwebwx-bin/login? Uuid = '. $ this-> _ logincode.' & tip = 1 & _ = '. $ t;
$ Send_snoopy = new Snoopy;
$ Send_snoopy-> referer = "https://wx.qq.com /";
$ Send_snoopy-> fetch ($ url );
$ Result = $ send_snoopy-> results;
$ This-> log ('step1: '. $ result );
If ($ result ){
Preg_match ("/window \. code = (\ d +)/", $ result, $ matches );
If (count ($ matches)> 1 ){
$ Status = intval ($ matches [1]);
If ($ status = 201) $ _ SESSION ['login _ step'] = 1;
If ($ status = 200 ){
Preg_match ("/ticket = ([0-9a-z-_] +) & lang = zh_CN & scan = (\ d +)/", $ result, $ matches );
$ This-> log ('step2: '. print_r ($ matches, true ));
If (count ($ matches)> 1 ){
$ Ticket = $ matches [1];
$ Scan = $ matches [2];
$ Loginurl = 'https: // wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage? Ticket = '. $ ticket.' & lang = zh_CN & scan = '. $ scan.' & fun = new ';
$ Send_snoopy = new Snoopy;
$ Send_snoopy-> referer = "https://wx.qq.com /";
$ Send_snoopy-> fetch ($ loginurl );
$ This-> log ('step3: '. print_r ($ send_snoopy-> headers, true ));
Foreach ($ send_snoopy-> headers as $ key => $ value ){
$ Value = trim ($ value );
If (strpos ($ value, 'set-Cookie :')! = False ){
$ Tmp = str_replace ("Set-Cookie:", "", $ value );
$ Tmp = str_replace ("Path =/", "", $ tmp );
$ Tmp = str_replace ("Domain = .qq.com;", "", $ tmp );
$ Cookie. = $ tmp;
}
}
$ Cookie. = "Domain = .qq.com ;";
$ This-> cookie = $ cookie;
$ This-> saveCookie ($ this-> _ cookiename, $ this-> cookie );
}
}
Return $ status;
}
}
Return false;
}
/**
* Obtain the logon cookie.
*
* @ Param bool $ is_array: whether to return a value. The default value is no. The returned string is
* @ Return string | array
*/
Public function get_login_cookie ($ is_array = false ){
If (! $ Is_array) return $ this-> cookie;
$ C_arr = explode (';', $ this-> cookie );
$ Cookie = array ();
Foreach ($ c_arr as $ item ){
$ Kitem = explode ('=', trim ($ item ));
If (count ($ kitem)> 1 ){
$ Key = trim ($ kitem [0]);
$ Val = trim ($ kitem [1]);
If (! Empty ($ val) $ cookie [$ key] = $ val;
}
}
Return $ cookie;
}
/**
* Obtain user login information after authorized login
*/
Public function get_login_info (){
If (! $ This-> cookie) return false;
$ T = time (). strval (mt_rand (100,999 ));
$ Send_snoopy = new Snoopy;
$ Submit = 'https: // wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit? R = '. $ t;
$ Send_snoopy-> rawheaders ['cookies'] = $ this-> Cookie;
$ Send_snoopy-> referer = "https://wx.qq.com /";
$ Send_snoopy-> submit ($ submit, array ());
$ This-> log ('login _ info: '. $ send_snoopy-> results );
$ Result = json_decode ($ send_snoopy-> results, true );
If ($ result ['baseresponse'] ['ret '] <0) return false;
$ This-> _ login_user = $ result ['user'];
Return $ result;
}
/**
* Get the Avatar
* @ Param string $ url refers to the Avatar address obtained from the user information interface.
*/
Public function get_avatar ($ url ){
If (! $ This-> cookie) return false;
If (strpos ($ url, 'HTTP ') = false ){
$ Url = 'HTTP: // wx.qq.com '. $ url;
}
$ Send_snoopy = new Snoopy;
$ Send_snoopy-> rawheaders ['cookies'] = $ this-> Cookie;
$ Send_snoopy-> referer = "https://wx.qq.com /";
$ Send_snoopy-> fetch ($ url );
$ Result = $ send_snoopy-> results;
If ($ result)
Return $ result;
Else
Return false;
}
/**
* Log out of the current Login User
*/
Public function logout (){
If (! $ This-> cookie) return false;
Preg_match ("/wxuin = (\ w +);/", $ this-> cookie, $ matches );
If (count ($ matches)> 1) $ uid = $ matches [1];
Preg_match ("/wxsid = (\ w +);/", $ this-> cookie, $ matches );
If (count ($ matches)> 1) $ sid = $ matches [1];
$ This-> log ('logout: uid = '. $ uid.'; sid = '. $ sid );
$ Send_snoopy = new Snoopy;
$ Submit = 'https: // wx.qq.com/cgi-bin/mmwebwx-bin/webwxlogout? Redirect = 1 & type = 1 ';
$ Send_snoopy-> rawheaders ['cookies'] = $ this-> Cookie;
$ Send_snoopy-> referer = "https://wx.qq.com /";
$ Send_snoopy-> submit ($ submit, array ('uin' => $ uid, 'sid '=> $ sid ));
$ This-> deleteCookie ($ this-> _ cookiename );
Return true;
}
}
,