Пример #1
0
 /**
  * General data send/request method.
  * 
  * @param str $method 
  *    The data communication method.	 
  * @param str $url 
  *    The Linkedin API endpoint to connect with.
  * @param str $data
  *    [OPTIONAL] The data to send to LinkedIn.
  * @param arr $parameters 
  *    [OPTIONAL] Addition OAuth parameters to send to LinkedIn.
  *        
  * @return arr 
  *    Array containing:
  * 
  *           array(
  *             'info'      =>	Connection information,
  *             'linkedin'  => LinkedIn response,  
  *             'oauth'     => The OAuth request string that was sent to LinkedIn	 
  *           )	 
  */
 protected function fetch($method, $url, $data = NULL, $parameters = array())
 {
     // check for cURL
     if (!extension_loaded('curl')) {
         // cURL not present
         throw new SMAPLinkedInException('SMAPLinkedIn->fetch(): PHP cURL extension does not appear to be loaded/present.');
     }
     try {
         // generate OAuth values
         $oauth_consumer = new SMAPOAuthConsumer($this->getApplicationKey(), $this->getApplicationSecret(), $this->getCallbackUrl());
         $oauth_token = $this->getToken();
         $oauth_token = !is_null($oauth_token) ? new SMAPOAuthToken($oauth_token['oauth_token'], $oauth_token['oauth_token_secret']) : NULL;
         $defaults = array('oauth_version' => self::_API_OAUTH_VERSION);
         $parameters = array_merge($defaults, $parameters);
         // generate OAuth request
         $oauth_req = SMAPOAuthRequest::from_consumer_and_token($oauth_consumer, $oauth_token, $method, $url, $parameters);
         $oauth_req->sign_request(new SMAPOAuthSignatureMethod_HMAC_SHA1(), $oauth_consumer, $oauth_token);
         // start cURL, checking for a successful initiation
         if (!($handle = curl_init())) {
             // cURL failed to start
             throw new SMAPLinkedInException('SMAPLinkedIn->fetch(): cURL did not initialize properly.');
         }
         // set cURL options, based on parameters passed
         curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $method);
         curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
         //curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
         curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, get_option('xyz_smap_peer_verification') == '1' ? TRUE : FALSE);
         curl_setopt($handle, CURLOPT_URL, $url);
         curl_setopt($handle, CURLOPT_VERBOSE, FALSE);
         // configure the header we are sending to LinkedIn - http://developer.linkedin.com/docs/DOC-1203
         $header = array($oauth_req->to_header(self::_API_OAUTH_REALM));
         //       print_r($header);
         //       die;
         if (is_null($data)) {
             // not sending data, identify the content type
             $header[] = 'Content-Type: text/plain; charset=UTF-8';
             switch ($this->getResponseFormat()) {
                 case self::_RESPONSE_JSON:
                     $header[] = 'x-li-format: json';
                     break;
                 case self::_RESPONSE_JSONP:
                     $header[] = 'x-li-format: jsonp';
                     break;
             }
         } else {
             $header[] = 'Content-Type: text/xml; charset=UTF-8';
             curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
         }
         curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
         // set the last url, headers
         $this->last_request_url = $url;
         $this->last_request_headers = $header;
         // gather the response
         $return_data['linkedin'] = curl_exec($handle);
         $return_data['info'] = curl_getinfo($handle);
         $return_data['oauth']['header'] = $oauth_req->to_header(self::_API_OAUTH_REALM);
         $return_data['oauth']['string'] = $oauth_req->base_string;
         // check for throttling
         if (self::isThrottled($return_data['linkedin'])) {
             throw new SMAPLinkedInException('SMAPLinkedIn->fetch(): throttling limit for this user/application has been reached for LinkedIn resource - ' . $url);
         }
         //TODO - add check for NO response (http_code = 0) from cURL
         // close cURL connection
         curl_close($handle);
         // no exceptions thrown, return the data
         return $return_data;
     } catch (OAuthException $e) {
         // oauth exception raised
         throw new SMAPLinkedInException('OAuth exception caught: ' . $e->getMessage());
     }
 }
Пример #2
0
 /**
  * pretty much a helper function to set up the request
  */
 public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters = NULL)
 {
     @$parameters or $parameters = array();
     $defaults = array("oauth_version" => SMAPOAuthRequest::$version, "oauth_nonce" => SMAPOAuthRequest::generate_nonce(), "oauth_timestamp" => SMAPOAuthRequest::generate_timestamp(), "oauth_consumer_key" => $consumer->key);
     if ($token) {
         $defaults['oauth_token'] = $token->key;
     }
     $parameters = array_merge($defaults, $parameters);
     return new SMAPOAuthRequest($http_method, $http_url, $parameters);
 }