nsx_LinkedIn

The NextScripts: Social Networks Auto-Poster nsx LinkedIn class.

Defined (1)

The class is defined in the following location(s).

/inc-cl/apis/liOAuth.php  
  1. class nsx_LinkedIn { 
  2. public $base_url = "http://api.linkedin.com"; 
  3. public $secure_base_url = "https://api.linkedin.com"; 
  4. public $oauth_callback = "oob"; 
  5. public $consumer; 
  6. public $request_token; 
  7. public $access_token; 
  8. public $oauth_verifier; 
  9. public $signature_method; 
  10. public $request_token_path; 
  11. public $access_token_path; 
  12. public $authorize_path; 
  13. public $debug = false; 
  14. public $http_code; 
  15.  
  16. function __construct($consumer_key, $consumer_secret, $oauth_callback = NULL) { 
  17.  
  18. if($oauth_callback) { 
  19. $this->oauth_callback = $oauth_callback; 
  20.  
  21. $this->consumer = new nsx_trOAuthConsumer($consumer_key, $consumer_secret, $this->oauth_callback); 
  22. $this->signature_method = new nsx_trOAuthSignatureMethod_HMAC_SHA1(); 
  23. $this->request_token_path = $this->secure_base_url . "/uas/oauth/requestToken?scope=r_basicprofile+r_emailaddress+w_share"; 
  24. $this->access_token_path = $this->secure_base_url . "/uas/oauth/accessToken"; 
  25. $this->authorize_path = $this->secure_base_url . "/uas/oauth/authorize"; 
  26.  
  27.  
  28. function getRequestToken() { 
  29. $consumer = $this->consumer; 
  30. $request = nsx_trOAuthRequest::from_consumer_and_token($consumer, NULL, "GET", $this->request_token_path); 
  31. $request->set_parameter("oauth_callback", $this->oauth_callback); 
  32. $request->sign_request($this->signature_method, $consumer, NULL); // prr($request); die(); 
  33. $headers = Array(); 
  34. $url = $request->to_url(); // echo "^^^^^"; prr($url);  
  35. $response = $this->httpRequest($url, $headers, "GET"); //prr($response);  
  36. if ($response!='') $this->http_code = 200;  
  37. parse_str($response, $response_params); //prr($response_params); echo "!!!!"; 
  38. if (is_array($response_params) && !empty($response_params['oauth_problem'])) return print_r($response, true); 
  39. $this->request_token = new nsx_trOAuthConsumer($response_params['oauth_token'], $response_params['oauth_token_secret'], 1); return $this->request_token; 
  40.  
  41. function generateAuthorizeUrl() { 
  42. $consumer = $this->consumer; 
  43. $request_token = $this->request_token; 
  44. return $this->authorize_path . "?oauth_token=" . $request_token->key; 
  45.  
  46. function getAccessToken($oauth_verifier) { 
  47. $request = nsx_trOAuthRequest::from_consumer_and_token($this->consumer, $this->request_token, "GET", $this->access_token_path); 
  48. $request->set_parameter("oauth_verifier", $oauth_verifier); 
  49. $request->sign_request($this->signature_method, $this->consumer, $this->request_token); 
  50. $headers = Array(); 
  51. $url = $request->to_url(); // echo "=========="; 
  52. $response = $this->httpRequest($url, $headers, "GET"); //prr($request); 
  53. parse_str($response, $response_params); // prr($response_params); 
  54. if($debug) { 
  55. echo $response . "\n"; 
  56. $this->access_token = new nsx_trOAuthConsumer($response_params['oauth_token'], $response_params['oauth_token_secret'], 1); 
  57.  
  58. function getProfile($resource = "~") { 
  59. $profile_url = $this->base_url . "/v1/people/" . $resource; 
  60. $request = nsx_trOAuthRequest::from_consumer_and_token($this->consumer, $this->access_token, "GET", $profile_url); 
  61. $request->sign_request($this->signature_method, $this->consumer, $this->access_token); // prr($request); prr($profile_url); die(); 
  62. $auth_header = $request->to_header("https://api.linkedin.com"); # this is the realm 
  63. # This PHP library doesn't generate the header correctly when a realm is not specified. 
  64. # Make sure there is a space and not a comma after OAuth 
  65. // $auth_header = preg_replace("/Authorization\: OAuth\, /", "Authorization: OAuth ", $auth_header); 
  66. // # Make sure there is a space between OAuth attribute 
  67. // $auth_header = preg_replace('/\"\, /', '", ', $auth_header); 
  68. if ($debug) { 
  69. echo $auth_header; 
  70. // $response will now hold the XML document 
  71. $response = $this->httpRequest($profile_url, $auth_header, "GET"); 
  72. return $response; 
  73.  
  74. function postShare($msg, $title='', $url='', $imgURL='', $dsc='') { $status_url = $this->base_url . "/v1/people/~/shares";  
  75. $dsc = nxs_decodeEntitiesFull(strip_tags($dsc)); $msg = strip_tags(nxs_decodeEntitiesFull($msg)); $title = nxs_decodeEntitiesFull(strip_tags($title)); 
  76. $xml = '<?xml version="1.0" encoding="UTF-8"?><share><comment>'.htmlspecialchars($msg, ENT_NOQUOTES, "UTF-8").'</comment>'. 
  77. ($url!=''?'<content><title>'.htmlspecialchars($title, ENT_NOQUOTES, "UTF-8").'</title><submitted-url>'.$url.'</submitted-url>'.(!empty($imgURL)?'<submitted-image-url>'.$imgURL.'</submitted-image-url>':'').'<description>'.htmlspecialchars($dsc, ENT_NOQUOTES, "UTF-8").'</description></content>':''). 
  78. '<visibility><code>anyone</code></visibility></share>'; 
  79. $request = nsx_trOAuthRequest::from_consumer_and_token($this->consumer, $this->access_token, "POST", $status_url); 
  80. $request->sign_request($this->signature_method, $this->consumer, $this->access_token); 
  81. $auth_header = $request->to_header("https://api.linkedin.com"); 
  82. //if ($debug) echo $auth_header . "\n";  
  83. $response = $this->httpRequest($status_url, $auth_header, "POST", $xml);  
  84. return $response; 
  85.  
  86. function setStatus($status) { 
  87. $status_url = $this->base_url . "/v1/people/~/current-status"; 
  88. //echo "Setting status...\n"; 
  89. $xml = "<current-status>" . htmlspecialchars($status, ENT_NOQUOTES, "UTF-8") . "</current-status>"; 
  90. //echo $xml . "\n"; 
  91. $request = nsx_trOAuthRequest::from_consumer_and_token($this->consumer, $this->access_token, "PUT", $status_url); 
  92. $request->sign_request($this->signature_method, $this->consumer, $this->access_token); 
  93. $auth_header = $request->to_header("https://api.linkedin.com"); 
  94. if ($debug) { 
  95. echo $auth_header . "\n"; 
  96. $response = $this->httpRequest($status_url, $auth_header, "PUT", $xml); // prr($response); 
  97. return $response; 
  98.  
  99.  
  100. function httpRequest($url, $auth_header, $method, $body = NULL) { // $this->debug = true; //if (!is_array($auth_header)) $auth_header = array($auth_header); 
  101. if (!is_array($auth_header)) $auth_header = array($auth_header);  
  102. if (!$method) $method = "GET"; $curl = curl_init(); 
  103. curl_setopt($curl, CURLOPT_URL, $url); 
  104. curl_setopt($curl, CURLOPT_HEADER, 0); 
  105. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  106. curl_setopt($curl, CURLOPT_HTTPHEADER, $auth_header); // Set the headers. 
  107.  
  108. if ($body) { $auth_header[] = "Content-Type: text/xml;charset=utf-8"; 
  109. curl_setopt($curl, CURLOPT_POST, 1); 
  110. curl_setopt($curl, CURLOPT_POSTFIELDS, $body); 
  111. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); 
  112. curl_setopt($curl, CURLOPT_HTTPHEADER, $auth_header);  
  113. global $nxs_skipSSLCheck; if ($nxs_skipSSLCheck===true) curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
  114. $data = curl_exec($curl); $errmsg = curl_error($curl); //prr($data);// die(); 
  115.  
  116. //## NextScripts Fix 
  117. if (curl_errno($curl) == 60 || stripos($errmsg, 'SSL')!==false) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $data = curl_exec($curl);} 
  118. if (curl_errno($curl) > 0) { $err = curl_errno($curl); $errmsg = curl_error($curl); prr($errmsg); prr($err);}  
  119. //## /NextScripts Fix  
  120. $header = curl_getinfo($curl); curl_close($curl);// prr($header); 
  121.  
  122. if ($this->debug) echo $data . "\n";  
  123. if (trim($data)=='' && ($header['http_code']=='201' || $header['http_code']=='200' || $header['http_code']=='202')) $data = '201'; 
  124. return $data;  
  125.