如何使用codeigniter链接用户配置文件

时间:2013-03-06 06:59:21

标签: codeigniter-2 linkedin

在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

}

1 个答案:

答案 0 :(得分: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);

}

这对你有用。