tp5 micro-channel authorization


protected $appid = '****************'; //微信 appid
protected $appsecrt = '******************'; //微信 appsecrt


// ----------- Silent authorization (can not get the user's nickname, avatar, to get the user's nickname and avatar to use the user to confirm the authorization)
public function getBaseInfo ()
{
// get code
$ redirect_uri = urlencode ( "http: //www.******.com/index/index/getWxCode"); // callback address
$ url = "https://open.weixin.qq.com/connect/oauth2/authorize ? appid = ". $ this-> appid." & redirect_uri = $ redirect_uri & response_type = code & scope = snsapi_base & state = 123 # wechat_redirect "; // state can arbitrarily
    $this->redirect($url, 302);// tp5的重定向方法 
}

public function getWxCode()
{
//获取access_token
//获取openID
$code = $_GET['code'];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=
" . $this->appid . "&secret=" . $this->appsecrt . "&code=$code&grant_type=authorization_code";
    $r = http_curl($url);
$openid = $r['openid'];
$access_token = $r['access_token'];
var_dump('access_token是:' . $access_token . '=========' . 'openid是:' . $openid);
}


//-------------------------------end-----------------------------------


//-----------用户确认授权          
public function getCodeUserInfo($temp)
{
//获取code
$redirect_uri = urlencode("http://zs.zs13ce.gx.cn/index/index/getWxUserInfo");//回调地址
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid . "&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=$temp&connect_redirect=1#wechat_redirect"; //state 可任意
    $ this-> redirect ($ url, 302); // tp5 redirection method  
}


public function getWxUserInfo()
{
//换取网页授权access_token
//获取openid
$code = $this->request->param('code');
$state = $this->request->param('state');
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecrt . "&code=" . $code . "&grant_type=authorization_code";
$rjson = http_curl($url);
$access_token = $rjson['access_token'];//得到access_token ( 网页的)
$openId = $rjson['openid'];//得到openid
$userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=" . $this->. AppID " & lang = zh_CN"; // get user information
        Result = http_curl $ ($ userInfoUrl); 
// var_dump ($ Result); // Print User details
// Die;
IF (empty ($ Result)) {
echo 0; Die;
}
$ User name :: = Db ( "User") -> WHERE ( "OpenID", $ Result [ "OpenID"]) -> Find ();
IF (empty ($ User)) {
// if there is no table, insert
}
$ the this -> _ User User $ =;
$ this-> setIsSq ($ User);
$ this-> the redirect (URL ($ State), 302); // redirection method TP5 
}




http_curl function ($ URL, Data = $ []) 
{
$ curl = curl_init (); // initialize
curl_setopt ($ curl, CURLOPT_URL, $ url); // set crawl URL
curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, to false) ; // https request does not verify the certificate and the hosts
curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, to false);
curl_setopt ($ curl, CURLOPT_HEADER, 0); // set the header information is output as a data stream is set to 1, will HTTP Do not print out the http header information to speed up the efficiency
curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // set the acquired information is returned in the form of a file stream, instead of being output. If the setting is 0, the print information is to true
curl_setopt ($ curl, CURLOPT_POSTFIELDS, json_encode ($ Data, to true));
$ Data = the curl_exec ($ curl); // Run
$ = Result of json_decode ($ Data, to true);
IF (Data == $ to false) {
echo "curl Error:" curl_error ($ curl);.
Exit ();
}
Curl_close ($ curl); // Close the URL request
return $ Result;
}

Guess you like

Origin www.cnblogs.com/j-jian/p/11908997.html