PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/demo-linkedin/company.php

https://bitbucket.org/insigngmbh/socialapi
PHP | 495 lines | 320 code | 55 blank | 120 comment | 56 complexity | c4675a219bdbe3e7c882c7e77e7ec4fc MD5 | raw file
  1. <?php
  2. /**
  3. * This file is used in conjunction with the 'Simple-LinkedIn' class, demonstrating
  4. * the basic functionality and usage of the library.
  5. *
  6. * COPYRIGHT:
  7. *
  8. * Copyright (C) 2011, fiftyMission Inc.
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a
  11. * copy of this software and associated documentation files (the "Software"),
  12. * to deal in the Software without restriction, including without limitation
  13. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14. * and/or sell copies of the Software, and to permit persons to whom the
  15. * Software is furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  26. * IN THE SOFTWARE.
  27. *
  28. * SOURCE CODE LOCATION:
  29. *
  30. * http://code.google.com/p/simple-linkedinphp/
  31. *
  32. * REQUIREMENTS:
  33. *
  34. * 1. You must have cURL installed on the server and available to PHP.
  35. * 2. You must be running PHP 5+.
  36. *
  37. * QUICK START:
  38. *
  39. * There are two files needed to enable LinkedIn API functionality from PHP; the
  40. * stand-alone OAuth library, and the Simple-LinkedIn library. The latest
  41. * version of the stand-alone OAuth library can be found on Google Code:
  42. *
  43. * http://code.google.com/p/oauth/
  44. *
  45. * The latest versions of the Simple-LinkedIn library and this demonstation
  46. * script can be found here:
  47. *
  48. * http://code.google.com/p/simple-linkedinphp/
  49. *
  50. * Install these two files on your server in a location that is accessible to
  51. * this demo script. Make sure to change the file permissions such that your
  52. * web server can read the files.
  53. *
  54. * Next, make sure the path to the LinkedIn class below is correct.
  55. *
  56. * Finally, read and follow the 'Quick Start' guidelines located in the comments
  57. * of the Simple-LinkedIn library file.
  58. *
  59. * @version 3.2.0 - November 8, 2011
  60. * @author Paul Mennega <paul@fiftymission.net>
  61. * @copyright Copyright 2011, fiftyMission Inc.
  62. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  63. */
  64. /**
  65. * Session existance check.
  66. *
  67. * Helper function that checks to see that we have a 'set' $_SESSION that we can
  68. * use for the demo.
  69. */
  70. function oauth_session_exists() {
  71. if((is_array($_SESSION)) && (array_key_exists('oauth', $_SESSION))) {
  72. return TRUE;
  73. } else {
  74. return FALSE;
  75. }
  76. }
  77. try {
  78. // include the LinkedIn class
  79. require_once('../linkedin_3.2.0.class.php');
  80. // start the session
  81. if(!session_start()) {
  82. throw new LinkedInException('This script requires session support, which appears to be disabled according to session_start().');
  83. }
  84. // display constants
  85. $API_CONFIG = array(
  86. 'appKey' => '<your application key here>',
  87. 'appSecret' => '<your application secret here>',
  88. 'callbackUrl' => NULL
  89. );
  90. define('CONNECTION_COUNT', 20);
  91. define('DEFAULT_COMPANY_SEARCH', 'Microsoft');
  92. define('PORT_HTTP', '80');
  93. define('PORT_HTTP_SSL', '443');
  94. define('UPDATE_COUNT', 10);
  95. // set index
  96. $_REQUEST[LINKEDIN::_GET_TYPE] = (isset($_REQUEST[LINKEDIN::_GET_TYPE])) ? $_REQUEST[LINKEDIN::_GET_TYPE] : '';
  97. switch($_REQUEST[LINKEDIN::_GET_TYPE]) {
  98. case 'initiate':
  99. /**
  100. * Handle user initiated LinkedIn connection, create the LinkedIn object.
  101. */
  102. // check for the correct http protocol (i.e. is this script being served via http or https)
  103. if($_SERVER['HTTPS'] == 'on') {
  104. $protocol = 'https';
  105. } else {
  106. $protocol = 'http';
  107. }
  108. // set the callback url
  109. $API_CONFIG['callbackUrl'] = $protocol . '://' . $_SERVER['SERVER_NAME'] . ((($_SERVER['SERVER_PORT'] != PORT_HTTP) || ($_SERVER['SERVER_PORT'] != PORT_HTTP_SSL)) ? ':' . $_SERVER['SERVER_PORT'] : '') . $_SERVER['PHP_SELF'] . '?' . LINKEDIN::_GET_TYPE . '=initiate&' . LINKEDIN::_GET_RESPONSE . '=1';
  110. $OBJ_linkedin = new LinkedIn($API_CONFIG);
  111. // check for response from LinkedIn
  112. $_GET[LINKEDIN::_GET_RESPONSE] = (isset($_GET[LINKEDIN::_GET_RESPONSE])) ? $_GET[LINKEDIN::_GET_RESPONSE] : '';
  113. if(!$_GET[LINKEDIN::_GET_RESPONSE]) {
  114. // LinkedIn hasn't sent us a response, the user is initiating the connection
  115. // send a request for a LinkedIn access token
  116. $response = $OBJ_linkedin->retrieveTokenRequest();
  117. if($response['success'] === TRUE) {
  118. // store the request token
  119. $_SESSION['oauth']['linkedin']['request'] = $response['linkedin'];
  120. // redirect the user to the LinkedIn authentication/authorisation page to initiate validation.
  121. header('Location: ' . LINKEDIN::_URL_AUTH . $response['linkedin']['oauth_token']);
  122. } else {
  123. // bad token request
  124. echo "Request token retrieval failed:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>";
  125. }
  126. } else {
  127. // LinkedIn has sent a response, user has granted permission, take the temp access token, the user's secret and the verifier to request the user's real secret key
  128. $response = $OBJ_linkedin->retrieveTokenAccess($_SESSION['oauth']['linkedin']['request']['oauth_token'], $_SESSION['oauth']['linkedin']['request']['oauth_token_secret'], $_GET['oauth_verifier']);
  129. if($response['success'] === TRUE) {
  130. // the request went through without an error, gather user's 'access' tokens
  131. $_SESSION['oauth']['linkedin']['access'] = $response['linkedin'];
  132. // set the user as authorized for future quick reference
  133. $_SESSION['oauth']['linkedin']['authorized'] = TRUE;
  134. // redirect the user back to the demo page
  135. header('Location: ' . $_SERVER['PHP_SELF']);
  136. } else {
  137. // bad token access
  138. echo "Access token retrieval failed:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>";
  139. }
  140. }
  141. break;
  142. case 'revoke':
  143. /**
  144. * Handle authorization revocation.
  145. */
  146. // check the session
  147. if(!oauth_session_exists()) {
  148. throw new LinkedInException('This script requires session support, which doesn\'t appear to be working correctly.');
  149. }
  150. $OBJ_linkedin = new LinkedIn($API_CONFIG);
  151. $OBJ_linkedin->setTokenAccess($_SESSION['oauth']['linkedin']['access']);
  152. $response = $OBJ_linkedin->revoke();
  153. if($response['success'] === TRUE) {
  154. // revocation successful, clear session
  155. session_unset();
  156. $_SESSION = array();
  157. if(session_destroy()) {
  158. // session destroyed
  159. header('Location: ' . $_SERVER['PHP_SELF']);
  160. } else {
  161. // session not destroyed
  162. echo "Error clearing user's session";
  163. }
  164. } else {
  165. // revocation failed
  166. echo "Error revoking user's token:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>";
  167. }
  168. break;
  169. case 'followCompany':
  170. /**
  171. * Handle company 'follows'.
  172. */
  173. // check the session
  174. if(!oauth_session_exists()) {
  175. throw new LinkedInException('This script requires session support, which doesn\'t appear to be working correctly.');
  176. }
  177. $OBJ_linkedin = new LinkedIn($API_CONFIG);
  178. $OBJ_linkedin->setTokenAccess($_SESSION['oauth']['linkedin']['access']);
  179. if(!empty($_GET['nCompanyId'])) {
  180. $response = $OBJ_linkedin->followCompany($_GET['nCompanyId']);
  181. if($response['success'] === TRUE) {
  182. // company 'followed'
  183. header('Location: ' . $_SERVER['PHP_SELF']);
  184. } else {
  185. // problem with 'follow'
  186. echo "Error 'following' company:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>";
  187. }
  188. } else {
  189. echo "You must supply a company ID to 'follow' a company.";
  190. }
  191. break;
  192. case 'unfollowCompany':
  193. /**
  194. * Handle company 'unfollows'.
  195. */
  196. // check the session
  197. if(!oauth_session_exists()) {
  198. throw new LinkedInException('This script requires session support, which doesn\'t appear to be working correctly.');
  199. }
  200. $OBJ_linkedin = new LinkedIn($API_CONFIG);
  201. $OBJ_linkedin->setTokenAccess($_SESSION['oauth']['linkedin']['access']);
  202. if(!empty($_GET['nCompanyId'])) {
  203. $response = $OBJ_linkedin->unfollowCompany($_GET['nCompanyId']);
  204. if($response['success'] === TRUE) {
  205. // company 'unfollowed'
  206. header('Location: ' . $_SERVER['PHP_SELF']);
  207. } else {
  208. // problem with 'unfollow'
  209. echo "Error 'unfollowing' company:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>";
  210. }
  211. } else {
  212. echo "You must supply a company ID to 'unfollow' a company.";
  213. }
  214. break;
  215. default:
  216. // nothing being passed back, display demo page
  217. // check PHP version
  218. if(version_compare(PHP_VERSION, '5.0.0', '<')) {
  219. throw new LinkedInException('You must be running version 5.x or greater of PHP to use this library.');
  220. }
  221. // check for cURL
  222. if(extension_loaded('curl')) {
  223. $curl_version = curl_version();
  224. $curl_version = $curl_version['version'];
  225. } else {
  226. throw new LinkedInException('You must load the cURL extension to use this library.');
  227. }
  228. ?>
  229. <!DOCTYPE html>
  230. <html lang="en">
  231. <head>
  232. <title>Simple-LinkedIn Demo &gt; Company</title>
  233. <meta charset="utf-8" />
  234. <meta name="viewport" content="width=device-width" />
  235. <meta name="description" content="A demonstration page for the Simple-LinkedIn PHP class." />
  236. <meta name="keywords" content="simple-linkedin,php,linkedin,api,class,library" />
  237. <style>
  238. body {font-family: Courier, monospace; font-size: 0.8em;}
  239. footer {margin-top: 2em; text-align: center;}
  240. pre {font-family: Courier, monospace; font-size: 0.8em;}
  241. </style>
  242. </head>
  243. <body>
  244. <h1><a href="/demo.php">Simple-LinkedIn Demo</a> &gt; <a href="<?php echo $_SERVER['PHP_SELF'];?>">Company</a></h1>
  245. <p>Copyright 2010 - 2011, Paul Mennega, fiftyMission Inc. &lt;paul@fiftymission.net&gt;</p>
  246. <p>Released under the MIT License - http://www.opensource.org/licenses/mit-license.php</p>
  247. <p>Full source code for both the Simple-LinkedIn class and this demo script can be found at:</p>
  248. <ul>
  249. <li><a href="http://code.google.com/p/simple-linkedinphp/">http://code.google.com/p/simple-linkedinphp/</a></li>
  250. </ul>
  251. <hr />
  252. <p style="font-weight: bold;">Demo using: Simple-LinkedIn v<?php echo LINKEDIN::_VERSION;?>, cURL v<?php echo $curl_version;?>, PHP v<?php echo phpversion();?></p>
  253. <ul>
  254. <li>Please note: The Simple-LinkedIn class requires PHP 5+</li>
  255. </ul>
  256. <hr />
  257. <?php
  258. $_SESSION['oauth']['linkedin']['authorized'] = (isset($_SESSION['oauth']['linkedin']['authorized'])) ? $_SESSION['oauth']['linkedin']['authorized'] : FALSE;
  259. if($_SESSION['oauth']['linkedin']['authorized'] === TRUE) {
  260. ?>
  261. <ul>
  262. <li><a href="#manage">Manage LinkedIn Authorization</a></li>
  263. <li><a href="../demo.php#application">Application Information</a></li>
  264. <li><a href="../demo.php#profile">Your Profile</a></li>
  265. <li><a href="#company">Company API</a>
  266. <ul>
  267. <li><a href="#companySpecific">Specific Company</a></li>
  268. <li><a href="#companyFollowed">Followed Companies</a></li>
  269. <li><a href="#companySuggested">Suggested Companies</a></li>
  270. <li><a href="#companySearch">Company Search</a></li>
  271. </ul>
  272. </li>
  273. </ul>
  274. <?php
  275. } else {
  276. ?>
  277. <ul>
  278. <li><a href="#manage">Manage LinkedIn Authorization</a></li>
  279. </ul>
  280. <?php
  281. }
  282. ?>
  283. <hr />
  284. <h2 id="manage">Manage LinkedIn Authorization:</h2>
  285. <?php
  286. if($_SESSION['oauth']['linkedin']['authorized'] === TRUE) {
  287. // user is already connected
  288. $OBJ_linkedin = new LinkedIn($API_CONFIG);
  289. $OBJ_linkedin->setTokenAccess($_SESSION['oauth']['linkedin']['access']);
  290. ?>
  291. <form id="linkedin_revoke_form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
  292. <input type="hidden" name="<?php echo LINKEDIN::_GET_TYPE;?>" id="<?php echo LINKEDIN::_GET_TYPE;?>" value="revoke" />
  293. <input type="submit" value="Revoke Authorization" />
  294. </form>
  295. <hr />
  296. <h2 id="company">Company API:</h2>
  297. <h3 id="companySpecific">Specific Company:</h3>
  298. <p>All about LinkedIn via the Company API:</p>
  299. <?php
  300. $OBJ_linkedin->setResponseFormat(LINKEDIN::_RESPONSE_XML);
  301. $response = $OBJ_linkedin->company('1337:(id,name,ticker,description,logo-url,locations:(address,is-headquarters))');
  302. if($response['success'] === TRUE) {
  303. $company = new SimpleXMLElement($response['linkedin']);
  304. ?>
  305. <div style=""><span style="font-weight: bold;"><?php echo $company->name;?> (<?php echo $company->ticker;?>)</span>&nbsp;<img src="<?php echo $company->{'logo-url'};?>" alt="<?php echo $company->name;?>" title="<?php echo $company->name;?>" style="vertical-align: middle;" /></div>
  306. <div style="margin: 0.5em 0 1em 2em;">
  307. <?php
  308. foreach($company->locations->location as $location) {
  309. if($location->{'is-headquarters'} == 'true') {
  310. $address = $location->address;
  311. ?>
  312. Headquarters: <?php echo $address->street1;?>, <?php echo $address->city;?>
  313. <?php
  314. }
  315. }
  316. ?>
  317. </div>
  318. <div style="margin: 0.5em 0 1em 2em;">
  319. Description: <?php echo $company->description;?>
  320. </div>
  321. <?php
  322. $response = $OBJ_linkedin->companyProducts('1337', ':(id,name,type,recommendations:(recommender,id))');
  323. if($response['success'] === TRUE) {
  324. $response['linkedin'] = new SimpleXMLElement($response['linkedin']);
  325. ?>
  326. <div style="margin: 0.5em 0 1em 2em;">
  327. Products: <pre><?php print_r($response['linkedin']);?></pre>
  328. </div>
  329. <?php
  330. } else {
  331. // request failed
  332. echo "Error retrieving company products:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>";
  333. }
  334. } else {
  335. // request failed
  336. echo "Error retrieving company information:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>";
  337. }
  338. ?>
  339. <hr />
  340. <h3 id="companyFollowed">Followed Companies:</h3>
  341. <p>Companies that you are currently following:</p>
  342. <?php
  343. $OBJ_linkedin->setResponseFormat(LINKEDIN::_RESPONSE_XML);
  344. $response = $OBJ_linkedin->followedCompanies();
  345. if($response['success'] === TRUE) {
  346. $followed = new SimpleXMLElement($response['linkedin']);
  347. if((int)$followed['total'] > 0) {
  348. foreach($followed->company as $company) {
  349. $cid = $company->id;
  350. $name = $company->name;
  351. ?>
  352. <div style=""><span style="font-weight: bold;"><?php echo $name;?></span></div>
  353. <div style="margin: 0.5em 0 1em 2em;">
  354. <a href="<?php echo $_SERVER['PHP_SELF'];?>?<?php echo LINKEDIN::_GET_TYPE;?>=unfollowCompany&amp;nCompanyId=<?php echo $cid;?>">Unfollow</a>
  355. </div>
  356. <?php
  357. }
  358. } else {
  359. // no companies follows
  360. echo '<div>You do not currently follow any companies.</div>';
  361. }
  362. } else {
  363. // request failed
  364. echo "Error retrieving followed companies:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>";
  365. }
  366. ?>
  367. <hr />
  368. <h3 id="companySuggested">Suggested Companies:</h3>
  369. <p>Companies that LinkedIn suggests that you follow:</p>
  370. <?php
  371. $OBJ_linkedin->setResponseFormat(LINKEDIN::_RESPONSE_XML);
  372. $response = $OBJ_linkedin->suggestedCompanies();
  373. if($response['success'] === TRUE) {
  374. $suggested = new SimpleXMLElement($response['linkedin']);
  375. if((int)$suggested['count'] > 0) {
  376. foreach($suggested->company as $company) {
  377. $cid = (string)$company->id;
  378. if(!empty($cid)) {
  379. $name = $company->name;
  380. ?>
  381. <div style=""><span style="font-weight: bold;"><?php echo $name;?></span></div>
  382. <div style="margin: 0.5em 0 1em 2em;">
  383. <?php
  384. echo '<a href="' . $_SERVER['PHP_SELF'] . '?' . LINKEDIN::_GET_TYPE . '=followCompany&amp;nCompanyId=' . $cid . '">Follow</a>';
  385. ?>
  386. </div>
  387. <?php
  388. }
  389. }
  390. } else {
  391. // no suggested follows
  392. echo '<div>LinkedIn is not suggesting any companies for you to follow at this time.</div>';
  393. }
  394. } else {
  395. // request failed
  396. echo "Error retrieving suggested companies:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>";
  397. }
  398. ?>
  399. <hr />
  400. <h3 id="companySearch">Company Search (by Relevance):</h3>
  401. <?php
  402. $OBJ_linkedin->setResponseFormat(LINKEDIN::_RESPONSE_JSON);
  403. $keywords = (isset($_GET['keywords'])) ? $_GET['keywords'] : DEFAULT_COMPANY_SEARCH;
  404. ?>
  405. <form action="<?php echo $_SERVER['PHP_SELF'];?>#companySearch" method="get">
  406. Search by Keywords: <input type="text" name="keywords" value="<?php echo $keywords;?>" /><input type="submit" value="Search" />
  407. </form>
  408. <?php
  409. $query = '?sort=relevance&keywords=' . urlencode($keywords);
  410. $response = $OBJ_linkedin->searchCompanies($query);
  411. if($response['success'] === TRUE) {
  412. echo "<pre>" . print_r($response['linkedin'], TRUE) . "</pre>";
  413. } else {
  414. // request failed
  415. echo "Error retrieving company search results:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>";
  416. }
  417. } else {
  418. // user isn't connected
  419. ?>
  420. <form id="linkedin_connect_form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
  421. <input type="hidden" name="<?php echo LINKEDIN::_GET_TYPE;?>" id="<?php echo LINKEDIN::_GET_TYPE;?>" value="initiate" />
  422. <input type="submit" value="Connect to LinkedIn" />
  423. </form>
  424. <?php
  425. }
  426. ?>
  427. <footer>
  428. <div>Copyright 2010 - 2011, fiftyMission Inc. (Paul Mennega &lt;<a href="mailto:paul@fiftymission.net">paul@fiftymission.net</a>&gt;)</div>
  429. <div>Released under the MIT License - <a href="http://www.opensource.org/licenses/mit-license.php">http://www.opensource.org/licenses/mit-license.php</a></div>
  430. </footer>
  431. </body>
  432. </html>
  433. <?php
  434. break;
  435. }
  436. } catch(LinkedInException $e) {
  437. // exception raised by library call
  438. echo $e->getMessage();
  439. }
  440. ?>