Table Of Contents:
We have seen in Part one of LinkedIn API tutorial how to connect to LinkedIn API via OAuth protocol and how to retreive users profile.
In Part 2 we will enrich our PHP LinkedIn Class with:
Here is the Screencast followed by a written tutorial:
http://blip.tv/file/get/Formatix-LinkedInAPIPhpZendOAuthUpdateStatus851.mp4
Tutorial
This Step has been described in details in our first Screencast Connect to LinkedIn API using Oauth . I strongly recommend you watch it before starting this one.
This Step can be divided in 5 phases:
public function getStatus()
{
// Set LinkedIn's GetStatus URI
$this->client->setUri('http://api.linkedin.com/v1/people/~:(current-status)');
// Set HTTP Method (GET, POST, PUT, or DELETE)
$this->client->setMethod(Zend_Http_Client::GET);
// Execute Request and get Response
$response = $this->client->request();
// Get the XML containing User's Status
$content = $response->getBody();
// Use Php simplexml to transform XML to a PHP Object
$xml = simplexml_load_string($content);
// Store Status
$status = (string) $xml->{'current-status'};
// Return Status as result
return $status;
}
public function updateStatus($status)
{
// Set LinkedIn's Set Status URI
$this->client->setUri('https://api.linkedin.com/v1/people/~/current-status');
// Set HTTP Method (GET, POST, PUT, or DELETE)
$this->client->setMethod(Zend_Http_Client::PUT );
// Create XML String containing Users Status to be passed with the PUT
$xml = $this->createXmlStatus($status);
// Attach the XML String to the HTTP request
$this->client->setRawData($xml,'text/xml');
// Set the Content Type in the HTTP Header
$this->client->setHeaders('Content-Type', 'text/xml');
// Execute Request
$this->client->request();
}
public function clearStatus()
{
// Set LinkedIn's Clear Status URI
$this->client->setUri('https://api.linkedin.com/v1/people/~/current-status');
// Set HTTP Method (GET, POST, PUT, or DELETE)
$this->client->setMethod(Zend_Http_Client::DELETE );
// Execute Request
$this->client->request();
}
As I’m doing this tutorial the very new Zend_OAuth Zend module still has some bugs which will probably be corrected when Zend_OAuth will officially join the Zend Framework that is to say in Zend Framework 1.10 .
In order to be able to do some HTTP “PUT” and “DELETE” we will have for now to modify 2 Zend_OAuth methods.
In the file incubator/library/Zend/Oauth/Client.php comment the if/else statement et call setRequestMethod regardless of $method value.
public function setMethod($method = self::GET)
{
$this->setRequestMethod($method);
//if ($method == self::GET) {
// $this->setRequestMethod(self::GET);
//} elseif($method == self::POST) {
// $this->setRequestMethod(self::POST);
//}
return parent::setMethod($method);
}
Then you will notice that setRequestMethod doesn’t support PUT nor DELETE either.
In file /incubator/library/Zend/Oauth/Config.php you will have to do something so that setRequestMethod will not trigger an error when given a PUT or a DELETE
You can make a quick and dirty workaround by commenting the control code as follows:
public function setRequestMethod($method)
{
$method = strtoupper($method);
//if (!in_array($method, array(Zend_Oauth::GET, Zend_Oauth::POST))) {
// require_once 'Zend/Oauth/Exception.php';
// throw new Zend_Oauth_Exception('Invalid method: '.$method);
//}
$this->_requestMethod = $method;
}
Or do that nicely by adding PUT and DELETE to the control array:
public function setRequestMethod($method)
{
$method = strtoupper($method);
if (!in_array($method, array(Zend_Oauth::GET, Zend_Oauth::POST, Zend_Oauth::PUT, Zend_Oauth::DELETE))) {
require_once 'Zend/Oauth/Exception.php';
throw new Zend_Oauth_Exception('Invalid method: '.$method);
}
$this->_requestMethod = $method;
}
The Test is divided in 3 phases
In our example we will
/*
*
* @abstract: Test LinkedIn's Php Class
* @author: Christophe Fiat
* @copyright: FormatiX.EU
*
*/
// Set Parameters
$params = Array( 'consumerKey' => 'YOUR_CONSUMER_KEY',
'consumerSecret' => 'YOUR_CONSUMER_SECRET_KEY',
'localUrl' => 'YOUR_Local_URL',
'callbackUrl' => 'YOUR_Callback_URL',
'zendPath' => 'YOUR_Zend_Path',
'zendIncubatorPath' => 'YOUR_Zend_Incubator_Path'
);
// Create an Instance of LinkedIn's PHP Class
$linkedin = new linkedIn($params);
// Connect to LinkedIn API Via OAuth
$linkedin->connect();
// Call whoAmI method to Get User's profile
$profilInfo =$linkedin->whoAmI();
// Display User's profile
echo'<h2>Prenom: '.$profilInfo['firstName']. '</h2>';
echo'<h2>Nom: '.$profilInfo['lastName']. '</h2>';
echo'<h2>Fonction: '.$profilInfo['headline']. '</h2>';
echo'<h2><a href="'.$profilInfo['profilUrl'].'">profil LinkedIn </a></h2>';
// Get User's Status
$userStatus = $linkedin->getStatus();
// Display User's Status
if(!empty($userStatus))
{
echo"<h2>Statut Of ".$profilInfo['firstName'] . ' ' . $profilInfo['lastName'] .":
$userStatus </h2>";
}
else
{
echo'<h2>No Status</h2>';
}
// Modify User's LinkedIn Status
$linkedin->updateStatus('Testing LinkedIn Status Update using Zend OAuth');
// Get The New Status
$userStatus = $linkedin->getStatus();
// Display User's LinkedIn Status
if(!empty($userStatus))
{
echo"<h2>Statut Of ".$profilInfo['firstName'] . ' ' . $profilInfo['lastName'] .":
$userStatus </h2>";
}
else
{
echo'<h2>No Status</h2>';
}
// Clear User's LinkedIn Status
$linkedin->clearStatus();
// Get Status
$userStatus = $linkedin->getStatus();
// Display User's Status
if(!empty($userStatus))
{
echo"<h2>Statut Of ".$profilInfo['firstName'] . ' ' . $profilInfo['lastName'] .":
$userStatus </h2>";
}
else
{
echo'<h2>No Status</h2>';
}
// The END! ;)
<?php
/*
*
* @abstract: Cette Classe sert a accéder a l'API de LinkedIn en utilisant l'Authentification Oauth Authentication Protocol
* @author: Christophe Fiat
* @version: 0.2 2009-12-06
* @copyright: FormatiX.EU
*
*/
class linkedIn
{
private $options;
private $consumer;
private $client;
private $token;
public function __construct($params)
{
// Add Zend and Zend Incubator Paths to the Include Path
set_include_path( $params['zendPath'] . PATH_SEPARATOR . $params['zendIncubatorPath'] . PATH_SEPARATOR . get_include_path() );
// Include Oauth Consumer class definition
require_once('Zend/Oauth/Consumer.php');
// Set Zend_Oauth_Consumer options
$this->options = array(
'version' => '1.0',
'localUrl' => $params['localUrl'],
'callbackUrl' => $params['callbackUrl'],
'requestTokenUrl' => 'https://api.linkedin.com/uas/oauth/requestToken',
'userAuthorisationUrl' => 'https://api.linkedin.com/uas/oauth/authorize',
'accessTokenUrl' => 'https://api.linkedin.com/uas/oauth/accessToken',
'consumerKey' => $params['consumerKey'],
'consumerSecret' => $params['consumerSecret']
);
// Instanciate Zend_Oauth_Consumer Class
$this->consumer = new Zend_Oauth_Consumer( $this->options );
}
public function connect()
{
// Start Session to be able to store Request Token and Access Token
session_start ();
if ( !isset ( $_SESSION ['ACCESS_TOKEN'] )) {
// We do not have any Access token Yet
if (! empty ( $_GET )) {
// But We have some parameters passed throw the URL
// Get the LinkedIn Access Token
$this->token = $this->consumer->getAccessToken ( $_GET, unserialize ( $_SESSION ['REQUEST_TOKEN'] ) );
// Store the LinkedIn Access Token
$_SESSION ['ACCESS_TOKEN'] = serialize ( $this->token );
} else {
// We have Nothing
// Start Requesting a LinkedIn Request Token
$this->token = $this->consumer->getRequestToken ();
// Store the LinkedIn Request Token
$_SESSION ['REQUEST_TOKEN'] = serialize ( $this->token );
// Redirect the Web User to LinkedIn Authentication Page
$this->consumer->redirect ();
}
} else {
// We've already Got a LinkedIn Access Token
// Restore The LinkedIn Access Token
$this->token = unserialize ( $_SESSION ['ACCESS_TOKEN'] );
}
// Use HTTP Client with built-in OAuth request handling
$this->client = $this->token->getHttpClient($this->options);
}
/*
* @abstract: This Method Grabs LinkedIn User Profile from LinkedIn API
*/
public function whoAmI()
{
// Set LinkedIn URI
$this->client->setUri('https://api.linkedin.com/v1/people/~');
// Set Method (GET, POST, PUT, or DELETE)
$this->client->setMethod(Zend_Http_Client::GET);
// Execute Request and get Response
$response = $this->client->request();
// Get the XML containing User's Profile
$content = $response->getBody();
// Uncomment Following Line To display XML result
// header('Content-Type: ' . $response->getHeader('Content-Type'));
// echo $content;
// exit;
// Use Php simplexml to transform XML to a PHP Object
$xml = simplexml_load_string($content);
// Uncomment Following Lines To display Simple XML Object Structure
// echo '<pre>';
// print_r($xml);
// echo'</pre>';
// Uncomment Following Lines To display LinkedIn User Profile
// echo 'First Name: ' . $xml->{'first-name'};
// echo '<br/>';
// echo 'Last Name: ' . $xml->{'last-name'};
// echo '<br/>';
// echo 'Headline: ' . $xml->{'headline'};
// Put the User Profile info in an Array
$info['firstName'] = (string) $xml->{'first-name'};
$info['lastName'] = (string) $xml->{'last-name'};
$info['headline'] = (string) $xml->{'headline'};
$info['profilUrl'] = (string) $xml->{'site-standard-profil-request'}->url;
// Return the Array as result
return $info;
}
/*
* @abstract: Creates THE XML string containing the User Status
*/
private function createXmlStatus($status)
{
$xml = '<?xml version="1.0" encoding="UTF-8"?><current-status>'.$status.'</current-status>';
return $xml;
}
/*
* @abstract: Gets the User's LinkedIn Status
*/
public function getStatus()
{
// Set LinkedIn's GetStatus URI
$this->client->setUri('http://api.linkedin.com/v1/people/~:(current-status)');
// Set HTTP Method (GET, POST, PUT, or DELETE)
$this->client->setMethod(Zend_Http_Client::GET);
// Execute Request and get Response
$response = $this->client->request();
// Get the XML containing User's Status
$content = $response->getBody();
// Use Php simplexml to transform XML to a PHP Object
$xml = simplexml_load_string($content);
// Store Status
$status = (string) $xml->{'current-status'};
// Return Status as result
return $status;
}
public function updateStatus($status)
{
// Set LinkedIn's Set Status URI
$this->client->setUri('https://api.linkedin.com/v1/people/~/current-status');
// Set HTTP Method (GET, POST, PUT, or DELETE)
$this->client->setMethod(Zend_Http_Client::PUT );
// Create XML String containing Users Status to be passed with the PUT
$xml = $this->createXmlStatus($status);
// Attach the XML String to the HTTP request
$this->client->setRawData($xml,'text/xml');
// Set the Content Type in the HTTP Header
$this->client->setHeaders('Content-Type', 'text/xml');
// Execute Request
$this->client->request();
}
public function clearStatus()
{
// Set LinkedIn's Clear Status URI
$this->client->setUri('https://api.linkedin.com/v1/people/~/current-status');
// Set HTTP Method (GET, POST, PUT, or DELETE)
$this->client->setMethod(Zend_Http_Client::DELETE );
// Execute Request
$this->client->request();
}
} // Class End
/*
*
* @abstract: Test LinkedIn's Php Class
* @author: Christophe Fiat
* @copyright: FormatiX.EU
*
*/
// Set Parameters
$params = Array( 'consumerKey' => 'YOUR_CONSUMER_KEY',
'consumerSecret' => 'YOUR_CONSUMER_SECRET_KEY',
'localUrl' => 'YOUR_Local_URL',
'callbackUrl' => 'YOUR_Callback_URL',
'zendPath' => 'YOUR_Zend_Path',
'zendIncubatorPath' => 'YOUR_Zend_Incubator_Path'
);
// Create an Instance of LinkedIn's PHP Class
$linkedin = new linkedIn($params);
// Connect to LinkedIn API Via OAuth
$linkedin->connect();
// Call whoAmI method to Get User's profile
$profilInfo =$linkedin->whoAmI();
// Display User's profile
echo'<h2>Prenom: '.$profilInfo['firstName']. '</h2>';
echo'<h2>Nom: '.$profilInfo['lastName']. '</h2>';
echo'<h2>Fonction: '.$profilInfo['headline']. '</h2>';
echo'<h2><a href="'.$profilInfo['profilUrl'].'">profil LinkedIn </a></h2>';
// Get User's Status
$userStatus = $linkedin->getStatus();
// Display User's Status
if(!empty($userStatus))
{
echo"<h2>Statut Of ".$profilInfo['firstName'] . ' ' . $profilInfo['lastName'] .":
$userStatus </h2>";
}
else
{
echo'<h2>No Status</h2>';
}
// Modify User's LinkedIn Status
$linkedin->updateStatus('Testing LinkedIn Status Update using Zend OAuth');
// Get The New Status
$userStatus = $linkedin->getStatus();
// Display User's LinkedIn Status
if(!empty($userStatus))
{
echo"<h2>Statut Of ".$profilInfo['firstName'] . ' ' . $profilInfo['lastName'] .":
$userStatus </h2>";
}
else
{
echo'<h2>No Status</h2>';
}
// Clear User's LinkedIn Status
$linkedin->clearStatus();
// Get Status
$userStatus = $linkedin->getStatus();
// Display User's Status
if(!empty($userStatus))
{
echo"<h2>Statut Of ".$profilInfo['firstName'] . ' ' . $profilInfo['lastName'] .":
$userStatus </h2>";
}
else
{
echo'<h2>No Status</h2>';
}
// The END! ;)
?>
You can Download the LinkedIn Class.
2 Responses
muthukumar
03|Feb|2010 1hi,
i need your help in terms of linkedin api to get the output using the Search API. I have used the above same application to login and it is working but the Search API does not seam to work when I send in the request. I will be of great help if you can help me on this since I am at my client place and not able to get the output.
Please reply me back as soon as possible for this … Please
Thanks a lot for this amazing video that you have given above but it wil be of great help if you can get me the Search API implemented in this..
Please help.
Thanks & Regards,
Muthukumar
Chris
10|Feb|2010 2Hi Muthukumar,
I haven’t tried the search API yet :(. So I am not able to give you a feedback right now on it and I’m by the way also very busy right now ;)
If you are really in a hurry I’m sure “Taylor Singletary” or some of linkedin folks can give you very usefull tips to solve your problem.
But I will plan to demonstrate LinkedIn Search API in the Next Screencast.
Best of luck.
Chris.
Leave a reply