PHP对接twitter三方登录-WEB版

1.https://developer.twitter.com/创建应用

放开想用的权限及配置回调地址和网址

2.下载类库 

链接:https://pan.baidu.com/s/16xDOUiLEIZuRSNt6Z9afdg 
提取码:aqkf 
复制这段内容后打开百度网盘手机App,操作更方便哦

3.授权链接

public function loginTwitter() { 
        require "../lib/internal/twitteroauth/twitteroauth.php";        
        $ttConfig = $this->snsaccount->twitterAccount();
        
        // Create TwitterOAuth obj instance
        $twitteroauth = new \TwitterOAuth('api_key', 'api_secret');
        // Requesting authentication tokens, the parameter is the URL we will be redirected to
        $redirectUrl = '回调地址';
        $request_token = $twitteroauth->getRequestToken($redirectUrl);
        
        // save to session
        $_SESSION['oauth_token'] = $request_token['oauth_token'];
        $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
        
        // no error
        if($twitteroauth->http_code==200){
            // Let's generate the URL and redirect
            $url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);     
           header('Location: '. $url);
        } else {
            // error
            $this->messageManager->addErrorMessage(__('twitter login fail'));
        }        
   }

4.回调方法

public function loginTwitterRedirect(){
    require "../lib/internal/twitteroauth/twitteroauth.php"; 
        $oauth_verifier = $this->getRequest()->getParam('oauth_verifier');
        if(!empty($oauth_verifier) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])){
            // The data is legal. Go ahead
            // TwitterOAuth Object instance, pay attention to the two newly added parameters
            $twitteroauth = new \TwitterOAuth('api_key', 'api_secret', $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
            // access token
            $access_token = $twitteroauth->getAccessToken($oauth_verifier);           
            $_SESSION['access_token'] = $access_token;
            // Get user information
            $auth_param = array(
                'include_email'=>true
            );
            $user_info = $twitteroauth->get('account/verify_credentials',$auth_param);      
            print_r($user_info); die;             
            $type = 'twitter';
            $client_id = $user_info['id'];
            $client_name = $user_info['screen_name'];            
            $client_email = $user_info['email'];
            $this->getCustomerBySns($type,$client_id,$client_name,$client_email);
        } else {
            // Incomplete data, go to the previous step
            $prev_url = $this->_url->getUrl('appcustomer/account/loginsns',['sns'=>'twitter']);
            header('Location: '.$prev_url);
        }
   }

 

参考:https://blog.51cto.com/dapingguo/469136

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值