<?php
/**
* This file defines the 'LinkedIn' class. This class is designed to be a
* simple, stand-alone implementation of the LinkedIn API functions.
*
* COPYRIGHT:
*
* Copyright (C) 2011, fiftyMission Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* SOURCE CODE LOCATION:
*
* http://code.google.com/p/simple-linkedinphp/
*
* REQUIREMENTS:
*
* 1. You must have cURL installed on the server and available to PHP.
* 2. You must be running PHP 5+.
*
* QUICK START:
*
* There are two files needed to enable LinkedIn API functionality from PHP; the
* stand-alone OAuth library, and this LinkedIn class. The latest version of
* the stand-alone OAuth library can be found on Google Code:
*
* http://code.google.com/p/oauth/
*
* Install these two files on your server in a location that is accessible to
* the scripts you wish to use them in. Make sure to change the file
* permissions such that your web server can read the files.
*
* Next, make sure the path to the OAuth library is correct (you can change this
* as needed, depending on your file organization scheme, etc).
*
* Finally, test the class by attempting to connect to LinkedIn using the
* associated demo.php page, also located at the Google Code location
* referenced above.
*
* RESOURCES:
*
* REST API Documentation: http://developer.linkedin.com/rest
*
* @version 3.3.0 - December 10, 2011
* @author Paul Mennega <paul@fiftymission.net>
* @copyright Copyright 2011, fiftyMission Inc.
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Source: http://code.google.com/p/oauth/
*
* Rename and move as needed, changing the require_once() call to the correct
* name and path.
*/
if(!extension_loaded('oauth')) {
// the PECL OAuth extension is not present, load our third-party OAuth library
require_once('OAuth.php');
} else {
// the PECL extension is present, which is not compatible with this library
throw new LinkedInException('Simple-LinkedIn: library not compatible with installed PECL OAuth extension. Please disable this extension to use the Simple-LinkedIn library.');
}
/**
* 'LinkedInException' class declaration.
*
* This class extends the base 'Exception' class.
*
* @access public
* @package classpackage
*/
class LinkedInException extends Exception {}
/**
* 'LinkedIn' class declaration.
*
* This class provides generalized LinkedIn oauth functionality.
*
* @access public
* @package classpackage
*/
class LinkedIn {
// api/oauth settings
const _DEFAULT_OAUTH_REALM = 'http://api.linkedin.com';
const _DEFAULT_OAUTH_VERSION = '1.0';
// the default response format from LinkedIn
const _DEFAULT_RESPONSE_FORMAT = 'xml';
// helper constants used to standardize LinkedIn <-> API communication. See demo page for usage.
const _GET_RESPONSE = 'lResponse';
const _GET_TYPE = 'lType';
// Invitation API constants.
const _INV_SUBJECT = 'Invitation to connect';
const _INV_BODY_LENGTH = 200;
// API methods
const _METHOD_TOKENS = 'POST';
// Network API constants.
const _NETWORK_LENGTH = 1000;
const _NETWORK_HTML = '<a>';
// response format type constants, see http://developer.linkedin.com/docs/DOC-1203
const _RESPONSE_JSON = 'JSON';
const _RESPONSE_JSONP = 'JSONP';
const _RESPONSE_XML = 'XML';
// Share API constants
const _SHARE_COMMENT_LENGTH = 700;
const _SHARE_CONTENT_TITLE_LENGTH = 200;
const _SHARE_CONTENT_DESC_LENGTH = 400;
// LinkedIn API end-points
const _URL_ACCESS = 'https://api.linkedin.com/uas/oauth/accessToken';
const _URL_API = 'https://api.linkedin.com';
/**
* @deprecated
*/
const _URL_AUTH = self::_URL_AUTHENTICATE;
const _URL_AUTHENTICATE = 'https://www.linkedin.com/uas/oauth/authenticate?oauth_token=';
const _URL_AUTHORIZE = 'https://www.linkedin.com/uas/oauth/authorize?oauth_token=';
const _URL_REQUEST = 'https://api.linkedin.com/uas/oauth/requestToken';
const _URL_REVOKE = 'https://api.linkedin.com/uas/oauth/invalidateToken';
// library version
const _VERSION = '3.3.0';
// oauth properties
protected $callback;
protected $token = NULL;
// application properties
protected $application_key,
$application_secret;
// the format of the data to return
protected $response_format = self::_DEFAULT_RESPONSE_FORMAT;
// last request fields
public $last_request_headers,
$last_request_url;
/**
* Create a LinkedIn object, used for OAuth-based authentication and
* communication with the LinkedIn API.
*
* @param arr $config
* The 'start-up' object properties:
* - appKey => The application's API key
* - appSecret => The application's secret key
* - callbackUrl => [OPTIONAL] the callback URL - only used to
* retrieve the request token.
*
* @return obj
* A new LinkedIn object.
*/
public function __construct($config) {
if(!is_array($config)) {
// bad data passed
throw new LinkedInException('LinkedIn->__construct(): bad data passed, $config must be of type array.');
}
$this->setApplicationKey($config['appKey']);
$this->setApplicationSecret($config['appSecret']);
if(array_key_exists('callbackUrl', $config)) {
$this->setCallbackUrl($config['callbackUrl']);
} else {
$this->setCallbackUrl(NULL);
}
}
/**
* The class destructor.
*
* Explicitly clears LinkedIn object from memory upon destruction.
*/
public function __destruct() {
unset($this);
}
/**
* Bookmark a job.
*
* Calling this method causes the current user to add a bookmark for the
* specified job:
*
* http://developer.linkedin.com/docs/DOC-1323
*
* @param str $jid
* Job ID you want to bookmark.
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*
* @since 3.1.0
*/
public function bookmarkJob($jid) {
// check passed data
if(!is_string($jid)) {
// bad data passed
throw new LinkedInException('LinkedIn->bookmarkJob(): bad data passed, $jid must be of type string.');
}
// construct and send the request
$query = self::_URL_API . '/v1/people/~/job-bookmarks';
$response = $this->fetch('POST', $query, '<job-bookmark><job><id>' . trim($jid) . '</id></job></job-bookmark>');
/**
* Check for successful request (a 201 response from LinkedIn server)
* per the documentation linked in m