0

我最近刚刚为我们的网站创建了一个“使用 Facebook 注册”按钮,我正在尝试使用 Twitter 实现同样的目标。

但是,我很难找到使用 Twitter 完成任务的精确分步说明。

我对 Facebook 所做的是:

// (1) - Create new Facebook object:
$facebook = new Facebook(array( 
    'appId'  => $this->config->item('facebook_app_id'),  
    'secret' => $this->config->item('facebook_app_secret')
));

// (2) - Get all fields relating to logged-in user:
$facebook_user = $facebook->getUser();

// (3) - Retrieve all fields which we need to register a user on OUR website:
if ($facebook_user){//if logged in to Facebook:
    $facebook_user_profile = $facebook->api('/me');
    $fb_email        = $facebook_user_profile["email"];
    $fb_first_name   = $facebook_user_profile["first_name"];
    $fb_last_name    = $facebook_user_profile["last_name"];
    // ...
}

问题 1:

如何使用 Twitter 实现相同的目标?

问题2:

我创建了一个应用程序(https://dev.twitter.com/apps/new),现在有 4 个代码(消费者密钥、消费者密钥、访问令牌、访问令牌密钥),我不知道这些代码是什么,但是我需要这 4 个代码来完成手头的任务吗?

任何建议或在线资源都会很棒...

4

1 回答 1

0

下载 tmhOauth 类https://github.com/themattharris/tmhOAuth

$settings['consumer_key']   = 'your_key';
$settings['consumer_secret']= 'your_secret';

parse_str( $_SERVER['QUERY_STRING'], $_REQUEST );

require 'twitter/tmhoauth.php';
require 'twitter/tmhUtilities.php';

$this->_twitter = new Tmhoauth($settings);

if ( isset($_SESSION['access_token']) ) { //if already logged
    $this->_twitter->config['user_token']  = $_SESSION['access_token']['oauth_token'];
    $this->_twitter->config['user_secret'] = $_SESSION['access_token']['oauth_token_secret'];

    $code = $this->_twitter->request('GET', $this->_twitter->url('1/account/verify_credentials'));
    if ($code == 200) {
        // save user data
    }
} elseif (isset($_REQUEST['oauth_verifier'])) { // we're being called back by Twitter
    $code = $this->_twitter->request('POST', $this->_twitter->url('oauth/access_token', ''), array(
        'oauth_verifier' => $_REQUEST['oauth_verifier'],
        'oauth_token' => $_REQUEST['oauth_token']
    ));

    if ($code == 200) {
        $_SESSION['access_token'] = $this->_twitter->extract_params($this->_twitter->response['response']);
        // save user data
    }
} else{ // login url for your "connect with twitter" button
    $here = tmhUtilities::php_self();
    $callback = isset($_REQUEST['oob']) ? 'oob' : $here;
    $params = array( 'oauth_callback' => $callback );

    $code = $this->_twitter->request('POST', $this->_twitter->url('oauth/request_token', ''), $params);

    if ($code == 200) {
        $test_this = $this->_twitter->extract_params($this->_twitter->response['response']);
        $_SESSION['oauth'] = $test_this;
        $method = isset($_REQUEST['authenticate']) ? 'authenticate' : 'authorize';
        $force  = isset($_REQUEST['force']) ? '&force_login=1' : '';
        $authurl = $this->_twitter->url("oauth/{$method}", '') .  "?oauth_token={$test_this['oauth_token']}{$force}";
        $this->_loginUrl = $authurl;
    }
}

无法使用 twitter api 检索用户的电子邮件地址。

于 2012-10-09T21:13:20.240 回答