LinkedIn Integration with Drupal

06 Aug

LinkedIn Integration with Drupal

in Drupal, Drupal, Drupal Planet

How to send Message or Invitation to LinkedIn friends using Linkedin account with Drupal?

 

1)     CreateLinkedIn Application

URL - https://www.linkedin.com/secure/developer

 

·       Create LinkedIn Application 

·       Get Api Key and Secret Key

 

2)      Use following Module for integration

·        Linkedin - http://drupal.org/project/linkedin/

·        OAuth- http://drupal.org/project/oauth

 

Set consumer key and secret Key then authenticate using OAuth. After authentication it will open a connection to LinkedIn. Once connected, you will be able to access the friends and their information.

function connection()

{

$consumer_key = ‘abcde’                      // app key

$consumer_secret = ‘xyz’  // secret key

Send token to oauth for authentication

$consumer = new OAuthConsumer($consumer_key, $consumer_secret, NULL);

$base_url = "https://api.linkedin.com/uas/oauth";

$url = $base_url . "/requestToken";

$signature = new OAuthSignatureMethod_HMAC_SHA1();

 $request = OAuthRequest::from_consumer_and_token($consumer, NULL, 'POST', $url);

   $request->sign_request($signature, $consumer, NULL);

    $header = $request->to_header("http://api.linkedin.com");

$provider = 'linkedin';

 $tokens = db_fetch_array(db_query("SELECT * FROM {linkedin_token} WHERE uid = 120 AND type = 'access'")); // get token key and token secret from query

        $token = array(

            'token_key' => $tokens['token_key'],

            'token_secret' => $tokens['token_secret'],

        ); // get login user token

$result = linkedin_get_fields('http://api.linkedin.com/v1/people/~/connections', $token);

 // $result is array of connection of friends

}

This function sends Message to Friends

Send message()

{

$base_url = "http://api.linkedin.com/v1/people/~/mailbox";

$signature = new OAuthSignatureMethod_HMAC_SHA1();

$consumer_key = ‘c2tqvnglgfyq’                      // app key

$consumer_secret = ‘hGEkm6Gdpc3ibjP9’  // secret key

 $consumer = new OAuthConsumer($consumer_key, $consumer_secret, NULL); 

$token = new OAuthConsumer($tokens['token_key'], $tokens['token_secret'], 1); // user key

$request = OAuthRequest::from_consumer_and_token($consumer, $token, "POST", $base_url);

// send URL

 $request->sign_request($signature, $consumer, $token);

 $header = $request->to_header("http://api.linkedin.com");

 $friends_id=’abcdf43’;

// send you message using xml format

$xml_message_body .= '<?xml version="1.0" encoding="UTF-8"?>';

$xml_message_body .= '<mailbox-item>';

 $xml_message_body .= '<recipients>';

 $xml_message_body .= '<recipient>';

 $xml_message_body .= '<person path="/people/' . $friends_id. '" />';

 $xml_message_body .= '</recipient>';

 $xml_message_body .= '</recipients>';

$xml_message_body .= '<subject>Flexibage Invitation</subject>';

 $xml_message_body .= '<body>http://flexibadge.10jumps.org</body>';

 $xml_message_body .= '</mailbox-item>';

$response = _linkedin_connections_http_request($base_url, $header, $xml_message_body);

}

The LinkedIn connection -  http request function  to post your request  to server.

function _linkedin_connections_http_request($url, $header, $body = NULL) {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));

    curl_setopt($ch, CURLOPT_URL, $url);

    if ($body) {

        curl_setopt($ch, CURLOPT_POST, 1);

        if ($body == 'token_request') {

            curl_setopt($ch, CURLOPT_POSTFIELDS, '');

        } else {

            curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

            curl_setopt($ch, CURLOPT_HTTPHEADER, array($header, 'Content-Type: text/xml;charset=utf-8'));

            curl_setopt($ch, CURLOPT_POST, 1);

            // It is important to set request to POST for sending messages.

            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

        }

    }

    // execute the curl call

    $output = curl_exec($ch);

    // get the http response code

    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    // close the curl connection

    curl_close($ch);

    // check for success. An httpd status of 201 inicates success:

    // see https://developer.linkedin.com/documents/messaging-between-connections-api

    if ($http_code == '201') {

        return $http_code;

    } else {

        // otherwise, return the xml $output

        return $output;

    }

}

copyright 2012 10jumps Llc.

copyright 2012 10jumps LLC.