微信小程式登陸demo
阿新 • • 發佈:2020-08-27
//封裝呼叫方法 也可以 file_get_contents($url);
public function getJson($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); }
進入正題 微信小程式是怎麼登陸的呢?
1。前端獲取微信的code碼
2。後臺通過Appid AppSecret 先獲取token
3。通過code Appid AppSecret 獲取openid
4。用過token openid 獲取使用者資訊
//這個只是驗證登陸
public function getParint() { $code= $_POST['code'];//接受code、使用者資訊 if(!$code){ msg(['code' => 20,'message' => "引數為空" ]); } $appid = C('APPID'); $AppSecret = C('AppSecret'); /*取得openid*/ $oauth2Url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$AppSecret&js_code=$code&grant_type=authorization_code"; $oauth2 = $this->getJson($oauth2Url); $openid = $oauth2['openid'];//openID $user = M('users'); $lists = []; $lists['openid'] = $openid; $lists['time'] = date('Y-m-d H:i:s');//登陸時間 $parteny = $user -> where(['openid'=>$openid]) -> find(); //如果是第一次進,新增 if(empty($parteny)){ $start = 1; $acces = $user->data($lists)->add();//記錄 if(!$acces){ $this->ajaxReturn(['code' => 0,'message' => "獲取資訊失敗",'openid'=>$openid ]); } $this->ajaxReturn(['code'=>1,'data'=>$openid,'state' => $start]); }else{ $start = 2; } $this->ajaxReturn(['code'=>1,'data'=>$parteny['openid'],'state' => $start]); }
/** * * 微信授權 */ public static function wechat_login($appid, $appsecret, $code) { $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code'; $token = json_decode(file_get_contents($token_url )); if (isset($token->errcode)) { return false; } $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . $appid . '&grant_type=refresh_token&refresh_token=' . $token->refresh_token; //轉成物件 $access_token = json_decode(file_get_contents($access_token_url)); if (isset($access_token->errcode)) { return false; } $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token->access_token . '&openid=' . $access_token->openid . '&lang=zh_CN'; //轉成物件 $user_info = json_decode(file_get_contents($user_info_url)); if (isset($user_info->errcode)) { return false; } $result = json_decode(json_encode($user_info), true);//返回的json陣列轉換成array陣列 return $result; }
其他網站的:
//返回內容:
[subscribe] => 1
[openid] => oZE7SjqjC_A88aGfaj_X8tJDC
[nickname] => 大仙
[sex] => 1
[language] => zh_CN
[city] => 大興
[province] => 北京
[country] => 中國 [headimgurl] function getUserInfoAll($code) { $appid = WX_APP_ID; $secret = WX_SECRET; //第一步:取全域性access_token $token = $global_token; //第二步:取得openid $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code"; $oauth2 = getJson($oauth2Url); //第三步:根據全域性access_token和openid查詢使用者資訊 $access_token = $token; $openid = $oauth2['openid']; $get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";//有subscribe $userinfo = getJson($get_user_info_url); session_start(); // session_id($userinfo['openid']); if($userinfo['subscribe']==1){ $_SESSION['userinfo']=$userinfo; } return $userinfo; } //返回無subscribe function getUserInfo($code) { $appid = WX_APP_ID; $appsecret = WX_SECRET; $access_token = ""; //根據code獲得Access Token $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code"; $access_token_json = https_request($access_token_url); $access_token_array = json_decode($access_token_json, true); $access_token = $access_token_array['access_token']; $openid = $access_token_array['openid']; //根據Access Token和OpenID獲得使用者資訊 $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid ";//無subscribe $userinfo = https_request($userinfo_url); $userinfo = json_decode($userinfo, true); return $userinfo; }