0

在linkedin完成验证后如何获取用户资料?我一直在谷歌搜索并找到了一些教程,但它们都没有工作,或者我应该说我无法让它们工作。下面是我的控制器。

function __construct(){
    parent::__construct();

    $this->config->load('linkedin');

    $this->data['consumer_key'] = $this->config->item('api_key');
    $this->data['consumer_secret'] = $this->config->item('secret_key');
    $this->data['callback_url'] = site_url() . 'main/linkedin_display';
}

function linkedin_request(){

    $this->load->library('linkedin', $this -> data);

    $token = $this->linkedin->get_request_token();

    $oauth_data = array(
        'oauth_request_token' => $token['oauth_token'],
        'oauth_request_token_secret' => $token['oauth_token_secret']
        );

    $this->session->set_userdata($oauth_data);

    $request_link = $this->linkedin->get_authorize_URL($token);

    header("Location: " . $request_link);

}

function linkedin_display{

    // get user details(first name, email etc) here

}
4

1 回答 1

2

将以下函数添加到您的linkedin 库中,以获取数据

function getData($url, $access_token)
{

    $request = OAuthRequest::from_consumer_and_token($this->consumer, $access_token, "GET", $url);
    $request->sign_request($this->method, $this->consumer, $access_token);
    $auth_header = $request->to_header("https://api.linkedin.com");

    $response = $this->httpRequest($shareUrl, $auth_header, "GET");

    return $response;
}

然后在您的控制器中创建一个函数,如下所示:

/**
 * Fetch linkedin profile
 */
function myprofile()
{
    $auth_data = $this->session->userdata('auth');

    $this->load->library('linkedin', $this->data);

    $status_response = $this->linkedin->getData('http://api.linkedin.com/v1/people/~', unserialize($auth_data['linked_in']));


    print_r($status_response);

}

这应该适合你。

于 2013-03-06T10:15:12.927 回答