This article brings you the content is about the TP5 Framework authorization login Code implementation, there is a certain reference value, the need for a friend can refer to, I hope you have some help.
New configuration file wechat.php
<?phpreturn [' OAuth ' = [' appid ' = ' * * * ', ' appsecret ' = ' * * * ',];
New extend\wechat\wechatoauth.php
<?phpnamespace wechat;/** * @package Authorization Controller */class wechatoauth{//Authorization configuration information private $wechat _config = [' AppID ' = = ', ' appsecret ', ' = ', '; Public Function __construct () {$this->wechat_config = $this->wechatconfig (); }/** * Get key configuration * @return [Type] array */Public function wechatconfig () {$wechat _config = Array_mer GE ($this->wechat_config,config (' Wechat.oauth ')); return $wechat _config; }/** * Get OpenID * @return string|mixed */Public Function getuseraccessuserinfo ($code = "") { if (empty ($code)) {$BASEURL = Request ()->url (true); $url = $this->getsingleauthorizeurl ($BASEURL, "123"); Header ("Location: $url"); Exit (); }else{$access _token = $this->getsingleaccesstoken ($code); return $this->getuserinfo ($access _token); }}/** * Authorized link * @paramString $redirect _uri the address to jump * @return [Type] Authorization link */Public function Getsingleauthorizeurl ($redir Ect_url = "", $state = ' 1 ') {$redirect _url = UrlEncode ($redirect _url); Return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=". $this->wechat_config[' AppID '). "&redirect_uri=". $redirect _url. " &response_type=code&scope=snsapi_userinfo&state={$state} #wechat_redirect "; }/** * Get token * @return [type] return token */Public Function Getsingleaccesstoken ($code) {$url = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid= '. $this->wechat_config[' AppID ']. ' &secret= '. $this->wechat_config[' Appsecret ']. ' &code= '. $code. ' &grant_type=authorization_code '; $access _token = $this->https_request ($url); return $access _token; /** * Send Curl Request * @param $url String * @param return array|mixed */Public function https_reques T ($url) {$curl = Curl_init (); curl_setopt ($curl, Curlopt_ssl_verifypeer, false); curl_setopt ($curl, Curlopt_url, $url); curl_setopt ($curl, Curlopt_returntransfer, 1); $AjaxReturn = curl_exec ($curl); Get Access_token and OpenID, convert to array $data = Json_decode ($AjaxReturn, true); Curl_close ($curl); return $data; }/** * @explain * Get user OpenID and user's number information via code * @return array|mixed * @remark * Gain access to the user's OpenID to determine user Whether there is data, you can skip to get access_token, you can continue to get Access_token * Access_token daily gain is limited, Access_token has a time limit, can be stored to the database 7200s. 7200s Access_token fail **/public Function GetUserInfo ($access _token = []) {if (! $access _token) { return [' Code ' = 0, ' msg ' = ' authorization failed ',]; } $userinfo _url = ' https://api.weixin.qq.com/sns/userinfo?access_token= '. $access _token[' Access_token ']. ' &openid= '. $access _token[' OpenID ']. ' &LANG=ZH_CN '; $userinfo _json = $this->https_request ($userinfo _url); Get the user's basic information and save the user's unique identity in the session if (! $userinfo _json) {return [' code ' = 0, ' msg ' = ' = ' failed to get user information! ', ]; } return $userinfo _json; }}
Controller call
Public Function Index () { $wchat = new \wechat\wechatoauth (); $code = Request ()->param (' Code ', ""); $user = $wchat->getuseraccessuserinfo ($code); }