PHP对接第三方 LINE 登录

1.LINE 官方文档:https://developers.line.biz/en/docs/line-login/getting-started/
2.开发者平台地址:https://developers.line.me/en/
3.获取的code 为一次性的,当你使用过这个code去获取token的时候 无论成功失败 都会在10分钟内失效! 10分钟之后 你才能用,当然 我想你没有时间去等,你可以再从登录开始,从新获取code!
注意 LINE 要配置的地址 必须是以 "https" 开头的, 当然如果你这边环境暂时没有配置好 这都没关系,
你可以正常配置你的回调地址在LINE上,之后 在登录line之后 会跳转一个错误的地址,
请仔细看,其实地址没有错,所以你只要点击查看地址的头部 把https的“s”去掉就可以看到回传的“code 和 state”

首先要做的第一步是 在自己的静态页面上放一个LINE的登录链接按钮  
按钮地址为:https://access.line.me/oauth2/v2.1/authorize?response_type=code&client_id={$Channel_ID}&redirect_uri={你的回调地址}&state={你的自定义状态码}&scope=openid%20profile  
在此之前 你 必须去开发者平台配置回调地址 
public function loginLine(){
        $params = $this->getRequest()->getParams();
        $lineConfig = $this->snsaccount->lineAccount();  //自定义LINE配置变量  
        $TokenParameter = array(
            'grant_type'=>'authorization_code',
            // linecode
            'code'=>isset($params['code'])?$params['code']:'', 
            //  Channel ID
            'client_id'=>$lineConfig['line_ChannelID'],
            // channel Secret
            'client_secret'=>$lineConfig['line_ChannelSecret'],
            // 
            'redirect_uri'=>$lineConfig['line_CallBack']
        );
        // $postdata = http_build_query($TokenParameter);
        //get access_token 
        $Tokencurl = 'https://api.line.me/oauth2/v2.1/token';
        $header = array('Content-type:application/x-www-form-urlencoded');              
        $tr=$this->http_post($Tokencurl,$TokenParameter,$header);      
        $TokenData = json_decode($tr,true);        
        $PersonalDataParameterUrl = 'https://api.line.me/v2/profile';
        $tp = $this->GetPersonalData($PersonalDataParameterUrl,$TokenData['access_token']);
        
        if($tp && count($tp) > 0){
            $type = 'line';
            $client_id = $tp['userId'];
            $client_name = $tp['displayName'];
            $client_email = '';
            $this->getCustomerBySns($type,$client_id,$client_name,$client_email);
        }else{
        //auth fail
            $this->messageManager->addErrorMessage('login fail');
            $resultRedirect = $this->resultRedirectFactory->create();            
            $resultRedirect->setUrl('*/*/create');
        }
    }
private function http_post($sUrl, $aData, $aHeader){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $sUrl);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($aData));
        $sResult = curl_exec($ch);
        if($sError=curl_error($ch)){
            die($sError);
        }
        curl_close($ch);
        return $sResult;
    
    }
    
    # GET
  private function GetPersonalData($url,$token){
        $headers[]  =  "Accept:application/json";
        $headers[]  =  "Authorization:Bearer ". $token;
        $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);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
    }

 

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值