PageRenderTime 70ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/demo-linkedin/jobs.php

https://bitbucket.org/insigngmbh/socialapi
PHP | 466 lines | 295 code | 52 blank | 119 comment | 53 complexity | cad1d4b0052683f7f9d2c2564c5da1af 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_JOB_SEARCH', 'Engineering');
  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 'bookmarkJob':
  170. /**
  171. * Handle job 'bookmarks'.
  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['nJobId'])) {
  180. $response = $OBJ_linkedin->bookmarkJob($_GET['nJobId']);
  181. if($response['success'] === TRUE) {
  182. // job 'bookmarked'
  183. header('Location: ' . $_SERVER['PHP_SELF']);
  184. } else {
  185. // problem with 'bookmark'
  186. echo "Error 'bookmarking' job:<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 job ID to 'bookmark' a job.";
  190. }
  191. break;
  192. case 'unbookmarkJob':
  193. /**
  194. * Handle job 'unbookmarks'.
  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['nJobId'])) {
  203. $response = $OBJ_linkedin->unbookmarkJob($_GET['nJobId']);
  204. if($response['success'] === TRUE) {
  205. // job 'unbookmarked'
  206. header('Location: ' . $_SERVER['PHP_SELF']);
  207. } else {
  208. // problem with 'unbookmark'
  209. echo "Error 'unbookmarking' job:<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 job ID to 'unbookmark' a job.";
  213. }
  214. default:
  215. // nothing being passed back, display demo page
  216. // check PHP version
  217. if(version_compare(PHP_VERSION, '5.0.0', '<')) {
  218. throw new LinkedInException('You must be running version 5.x or greater of PHP to use this library.');
  219. }
  220. // check for cURL
  221. if(extension_loaded('curl')) {
  222. $curl_version = curl_version();
  223. $curl_version = $curl_version['version'];
  224. } else {
  225. throw new LinkedInException('You must load the cURL extension to use this library.');
  226. }
  227. ?>
  228. <!DOCTYPE html>
  229. <html lang="en">
  230. <head>
  231. <title>Simple-LinkedIn Demo &gt; Jobs</title>
  232. <meta charset="utf-8" />
  233. <meta name="viewport" content="width=device-width" />
  234. <meta name="description" content="A demonstration page for the Simple-LinkedIn PHP class." />
  235. <meta name="keywords" content="simple-linkedin,php,linkedin,api,class,library" />
  236. <style>
  237. body {font-family: Courier, monospace; font-size: 0.8em;}
  238. footer {margin-top: 2em; text-align: center;}
  239. pre {font-family: Courier, monospace; font-size: 0.8em;}
  240. </style>
  241. </head>
  242. <body>
  243. <h1><a href="/demo.php">Simple-LinkedIn Demo</a> &gt; <a href="<?php echo $_SERVER['PHP_SELF'];?>">Jobs</a></h1>
  244. <p>Copyright 2010 - 2011, Paul Mennega, fiftyMission Inc. &lt;paul@fiftymission.net&gt;</p>
  245. <p>Released under the MIT License - http://www.opensource.org/licenses/mit-license.php</p>
  246. <p>Full source code for both the Simple-LinkedIn class and this demo script can be found at:</p>
  247. <ul>
  248. <li><a href="http://code.google.com/p/simple-linkedinphp/">http://code.google.com/p/simple-linkedinphp/</a></li>
  249. </ul>
  250. <hr />
  251. <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>
  252. <ul>
  253. <li>Please note: The Simple-LinkedIn class requires PHP 5+</li>
  254. </ul>
  255. <hr />
  256. <?php
  257. $_SESSION['oauth']['linkedin']['authorized'] = (isset($_SESSION['oauth']['linkedin']['authorized'])) ? $_SESSION['oauth']['linkedin']['authorized'] : FALSE;
  258. if($_SESSION['oauth']['linkedin']['authorized'] === TRUE) {
  259. ?>
  260. <ul>
  261. <li><a href="#manage">Manage LinkedIn Authorization</a></li>
  262. <li><a href="../demo.php#application">Application Information</a></li>
  263. <li><a href="../demo.php#profile">Your Profile</a></li>
  264. <li><a href="#jobs">Jobs API</a>
  265. <ul>
  266. <li><a href="#jobsBookmarked">Bookmarked Jobs</a></li>
  267. <li><a href="#jobsSuggested">Suggested Jobs</a></li>
  268. <li><a href="#jobsSearch">Jobs Search</a></li>
  269. </ul>
  270. </li>
  271. </ul>
  272. <?php
  273. } else {
  274. ?>
  275. <ul>
  276. <li><a href="#manage">Manage LinkedIn Authorization</a></li>
  277. </ul>
  278. <?php
  279. }
  280. ?>
  281. <hr />
  282. <h2 id="manage">Manage LinkedIn Authorization:</h2>
  283. <?php
  284. if($_SESSION['oauth']['linkedin']['authorized'] === TRUE) {
  285. // user is already connected
  286. $OBJ_linkedin = new LinkedIn($API_CONFIG);
  287. $OBJ_linkedin->setTokenAccess($_SESSION['oauth']['linkedin']['access']);
  288. ?>
  289. <form id="linkedin_revoke_form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
  290. <input type="hidden" name="<?php echo LINKEDIN::_GET_TYPE;?>" id="<?php echo LINKEDIN::_GET_TYPE;?>" value="revoke" />
  291. <input type="submit" value="Revoke Authorization" />
  292. </form>
  293. <hr />
  294. <h2 id="jobs">Jobs API:</h2>
  295. <h3 id="jobsBookmarked">Bookmarked Jobs:</h3>
  296. <p>Jobs that you currently have bookmarked:</p>
  297. <?php
  298. $OBJ_linkedin->setResponseFormat(LINKEDIN::_RESPONSE_XML);
  299. $response = $OBJ_linkedin->bookmarkedJobs();
  300. if($response['success'] === TRUE) {
  301. $bookmarked = new SimpleXMLElement($response['linkedin']);
  302. if((int)$bookmarked['total'] > 0) {
  303. $jobs = $bookmarked->{'job-bookmark'};
  304. foreach($jobs as $job) {
  305. $jid = $job->job->id;
  306. $title = $job->job->position->title;
  307. $company = $job->job->company->name;
  308. ?>
  309. <div style=""><span style="font-weight: bold;"><?php echo $title.": ".$company;?></span></div>
  310. <div style="margin: 0.5em 0 1em 2em;">
  311. <a href="<?php echo $_SERVER['PHP_SELF'];?>?<?php echo LINKEDIN::_GET_TYPE;?>=unbookmarkJob&amp;nJobId=<?php echo $jid;?>">Unbookmark</a>
  312. </div>
  313. <?php
  314. }
  315. } else {
  316. // no bookmarked jobs
  317. echo '<div>You have yet to bookmark any jobs.</div>';
  318. }
  319. } else {
  320. // request failed
  321. echo "Error retrieving followed companies:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>";
  322. }
  323. ?>
  324. <hr />
  325. <h3 id="jobsSuggested">Suggested Jobs:</h3>
  326. <p>Jobs that LinkedIn thinks you might be interested in:</p>
  327. <?php
  328. $field_selectors = ':(id,customer-job-code,active,posting-date,expiration-date,posting-timestamp,company:(id,name),position:(title,location,job-functions,industries,job-type,experience-level),skills-and-experience,description-snippet,description,salary,job-poster:(id,first-name,last-name,headline),referral-bonus,site-job-url,location-description)';
  329. $OBJ_linkedin->setResponseFormat(LINKEDIN::_RESPONSE_XML);
  330. $response = $OBJ_linkedin->suggestedJobs(":(jobs:(id,company,position:(title)))");
  331. if($response['success'] === TRUE) {
  332. $suggested = new SimpleXMLElement($response['linkedin']);
  333. $jobs = $suggested->jobs;
  334. if((int)$jobs['total'] > 0) {
  335. $job = $jobs->job;
  336. $count = 1;
  337. foreach($job as $job) {
  338. $jid = $job->id;
  339. $title = (string)$job->position->title;
  340. $company = $job->company->name;
  341. $poster = $job->{'job-poster'};
  342. $description = $job->{'description-snippet'};
  343. $location = $job->{'location-description'};
  344. ?>
  345. <div style=""><span style="font-weight: bold;"><?php echo $title.": ".$company;?></span></div>
  346. <?php
  347. if($count == 1) {
  348. $response = $OBJ_linkedin->job((string)$jid, $field_selectors);
  349. if($response['success'] === TRUE) {
  350. echo '<h4>Job Details:</h4>
  351. <pre>' . print_r(new SimpleXMLElement($response['linkedin']), TRUE) . '</pre>';
  352. } else {
  353. // request failed
  354. echo "Error retrieving jobs detailed information:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, true) . "</pre>";
  355. }
  356. }
  357. ?>
  358. <div style="margin: 0.5em 0 1em 2em;">
  359. <a href="<?php echo $_SERVER['PHP_SELF'];?>?<?php echo LINKEDIN::_GET_TYPE;?>=bookmarkJob&amp;nJobId=<?php echo $jid;?>">Bookmark</a>
  360. </div>
  361. <?php
  362. $count++;
  363. }
  364. } else {
  365. // no jobs suggested
  366. echo '<div>There are no suggested jobs for you at this time.</div>';
  367. }
  368. } else {
  369. // request failed
  370. echo "Error retrieving suggested companies:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>";
  371. }
  372. ?>
  373. <hr />
  374. <h3 id="jobsSearch">Job Search (by Relationship):</h3>
  375. <?php
  376. $OBJ_linkedin->setResponseFormat(LINKEDIN::_RESPONSE_JSON);
  377. $keywords = (isset($_GET['keywords'])) ? $_GET['keywords'] : DEFAULT_JOB_SEARCH;
  378. ?>
  379. <form action="<?php echo $_SERVER['PHP_SELF']?>#jobsSearch" method="get">
  380. Search by Keywords: <input type="text" name="keywords" value="<?php echo $keywords;?>" /><input type="submit" value="Search" />
  381. </form>
  382. <?php
  383. $query = '?keywords=' . urlencode($keywords) . '&sort=R';
  384. $response = $OBJ_linkedin->searchJobs($query);
  385. if($response['success'] === TRUE) {
  386. echo "<pre>" . print_r($response['linkedin'], TRUE) . "</pre>";
  387. } else {
  388. // request failed
  389. echo "Error retrieving job search results:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>";
  390. }
  391. } else {
  392. // user isn't connected
  393. ?>
  394. <form id="linkedin_connect_form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
  395. <input type="hidden" name="<?php echo LINKEDIN::_GET_TYPE;?>" id="<?php echo LINKEDIN::_GET_TYPE;?>" value="initiate" />
  396. <input type="submit" value="Connect to LinkedIn" />
  397. </form>
  398. <?php
  399. }
  400. ?>
  401. <footer>
  402. <div>Copyright 2010 - 2011, fiftyMission Inc. (Paul Mennega &lt;<a href="mailto:paul@fiftymission.net">paul@fiftymission.net</a>&gt;)</div>
  403. <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>
  404. </footer>
  405. </body>
  406. </html>
  407. <?php
  408. break;
  409. }
  410. } catch(LinkedInException $e) {
  411. // exception raised by library call
  412. echo $e->getMessage();
  413. }
  414. ?>