Please download the Snoopy class first
Copy CodeThe code is as follows:
/**
* Public Platform PHP-SDK
* Wechatauth for unofficial Login API
* Users to access the QR code provided by scanning the Web page to achieve the landing information
* The main implementation of the following functions:
* Get_login_code () to obtain a login authorization code, through the authorization code to obtain the QR code
* Get_code_image ($code = ") Converts the authorization code obtained above to the image QR code
* Verify_code () to determine if the login was successful, return 200 for final authorization success.
* Get_login_cookie () Call this method to obtain basic user information after successful authentication
* Sendnews ($account, $title, $summary, $content, $pic, $srcurl = ") send text messages to an account
* Get_avatar ($url) Get 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; Current login user, call Get_login_info after get
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 cookies to cache
* @param string $filename cache file name
* @param string $content file contents
* @return BOOL
*/
Public Function Savecookie ($filename, $content) {
Return file_put_contents ($filename, $content);
}
/**
* Read Cookie cache content
* @param string $filename cache file name
* @return A 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 Cookies
*/
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);
}
}
/**
* Get Authorization code for 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=wx782c26e4c19acffb&redirect_uri=https%3A%2F% 2fwx.qq.com%2fcgi-bin%2fmmwebwx-bin%2fwebwxnewloginpage&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 by 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/'. $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;
}
/**
* Access to login cookies
*
* @param bool $is Whether _array is returned numerically, default no, returns a string
* @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;
}
/**
* Access to 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[' Cookie ']= $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 Avatar
* @param string $url Pass in 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[' Cookie ']= $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[' 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;
}
}