Laravel实现第三方github登录方法

2021-07-30   阅读:1430   分类:后端    标签: Laravel

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;
	}
【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://tpxhm.com/adetail/753.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(1)

登录

遇见2021-07-30 14:49:01
666回复

简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×