1、在github官网添加应用
步骤参考这篇文章:Github 第三方登录接入:https://www.tpxhm.com/adetail/149.html
2、编写代码
/* * git登录 * */ public function githubLogin(){ $client_id= client_id; $client_secret= client_secret; header('location:https://github.com/login/oauth/authorize?client_id='.$client_id.'&client_secret='.$client_secret.'&scope=user:email'); } //Github回调 public function githubCallback(Request $request){ $client_id= client_id; $client_secret= client_secret; $code=$request->code; $access_token=$this->curl("https://github.com/login/oauth/access_token?client_id=".$client_id."&client_secret=".$client_secret."&code=".$code); // header("location:https://api.github.com/user?$token"); $info_url = 'https://api.github.com/user?'.$access_token; $arrydata = array(); $arrydata = $this->curl($info_url); parse_str($access_token,$arrydata); $token = $arrydata['access_token']; $url = "https://api.github.com/user?access_token=".$token; $headers[] = 'Authorization: token '.$token; $headers[] = "User-Agent: 简忆"; $result = $this->curl($info_url,[],$headers); $info = json_decode($result,true); if (isset($info['id'])) { dd($info) } } public function curl($url,$postData=[],$headers=[]){ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$url); //要访问的地址 curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //执行结果是否被返回,0返,1不返 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ch,CURLOPT_HTTPHEADER,$headers); if($postData){ curl_setopt($ch,CURLOPT_TIMEOUT,60); curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$postData); } if(curl_exec($ch)==false){ $data=''; }else{ $data=curl_multi_getcontent($ch); } curl_close($ch); return $data; }
文章评论(1)
遇见2021-07-30 14:49:01
666回复