Wednesday, April 30, 2014

LinkedIn API PHP Integration

Simple LinkedIn Share API Integration, This sample code has the ability to posts activities of users and share rich contents.

Get Your API key and App Secret 
http://developer.linkedin.com/

Simple-linkedinphp PHP SDK
http://code.google.com/p/simple-linkedinphp/


 Source Code
<?php session_start();

/**
 * Name         : LinkedIn API PHP Integration 
 * Description  : Simple LinkedIn Share API Integration
 * Author       : Ryan Tipones
 * Version      : 0.1
 * Author URI   : http://bikolexpressonline.com
 *  
 */

require_once "linkedin.php";

try {
    
    define('CALLBACK_URL', '<YOUR-CALLBACK-URL>');
    define('APP_KEY', '<YOUR-APP-KEY>');
    define('APP_SECRET', '<YOUR-APP-SECRET>');

    $linkedin = new LinkedIn(array(
                        'callbackUrl'   => CALLBACK_URL,
                        'appKey'        => APP_KEY,
                        'appSecret'     => APP_SECRET
                    )
            );

    if (isset($_REQUEST['oauth_verifier'])){
        
        /* Verify  Section */
        
        $response = $linkedin->retrieveTokenAccess(
                        $_SESSION['oauth_token'], 
                        $_SESSION['oauth_token_secret'], 
                        $_GET['oauth_verifier']
                );
        
        if($response['success'] === TRUE) {
             $_SESSION['oauth_access']  = $response['linkedin'];
        }
        
    }else{
        
        /* Auth Section */
        
        $response = $linkedin->retrieveTokenRequest();
        if($response['success'] === TRUE) {
                $_SESSION['oauth_token']        = $response['linkedin']['oauth_token'];
                $_SESSION['oauth_token_secret'] = $response['linkedin']['oauth_token_secret'];
                header('Location: ' . LINKEDIN::_URL_AUTH . $response['linkedin']['oauth_token']); 
            } else {
                throw new LinkedInException('Request token retrieval failed');
        }
    }
    
    
    /* Shared Section */
    
    $linkedin->setResponseFormat(LINKEDIN::_RESPONSE_JSON);
    $linkedin->setTokenAccess($_SESSION['oauth_access']);
    $response = $linkedin->profile('~/email-address');

    if($response['success']){
            $content                        = array();
            $content['comment']             = '<YOUR-POST-COMMENT>';
            $content['title']               = '<YOUR-POST-TITLE>';
            $content['submitted-url']       = '<YOUR-POST-SUBMITTED-URL>';
            $content['submitted-image-url'] = '<YOUR-POST-SUBMITTED-IMAGE-URL>';
            $content['description']         = '<YOUR-POST-DESCRIPTION>';
            $private                        = FALSE;

            $response = $linkedin->share('new', $content, $private); /* Post and Share to LinkedIn*/
            /* Debug */
            print_r($response);

    }else{
        throw new LinkedInException($response['error']);
    }

} catch(LinkedInException $e) {
  echo $e->getMessage();
}





Modify your linkedIn PHP SDK for Auth Permission Scope value under retrieveTokenRequest() Line 1851 add this permision scope=r_basicprofile+r_emailaddress+rw_nus

$response = $this->fetch(self::_METHOD_TOKENS, self::_URL_REQUEST.'?scope=r_basicprofile+r_emailaddress+rw_nus', NULL, $parameters);

1 comment:

  1. Nowadays various SMS API can be available in the market like SMS API PHP. through its integration, you can send SMS in bulk with a single link in your website, software, application.

    ReplyDelete

Genesis Framework for WordPress