protected function request($uri, $method = 'GET', $args = array()) { $url = $this->apiUrl . $uri; $request = new Request($method, $url, isset($args['params']) ? $args['params'] : array()); if (!isset($args['headers']['Accept'])) { $args['headers']['Accept'] = 'application/json'; } $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_POSTFIELDS, $request->to_postdata()); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $args['headers']); $response = array(); $response['return'] = $this->json_decode ? (array) json_decode(curl_exec($ch)) : curl_exec($ch); $response['info'] = curl_getinfo($ch); curl_close($ch); if ($response['info']['http_code'] == 401) { $this->authorized = $this->getAccessToken(); return $this->request($uri, $method, $args); } $payload = new Payload($request); $payload->setRawResponse($response); return $payload; }
protected function createSignedUrl($params = array()) { $url = $this->getEndpoint() . $this->resource; // Generate signature $consumer = new Consumer($this->getKey(), $this->getSecret()); $request = Request::from_consumer_and_token($consumer, null, 'GET', $url, $params); $request->sign_request(new HmacSha1(), $consumer, null); return $request->to_url(); }
/** * Make a request with this request handler * * @param string $method one of GET, POST * @param string $path the path to hit * @param array $options the array of params * * @return \stdClass response object */ public function request($method, $path, $options) { // Ensure we have options $options = $options ?: array(); // Take off the data param, we'll add it back after signing $file = isset($options['data']) ? $options['data'] : false; unset($options['data']); // Get the oauth signature to put in the request header $url = $this->baseUrl . $path; $oauth = \Eher\OAuth\Request::from_consumer_and_token($this->consumer, $this->token, $method, $url, $options); $oauth->sign_request($this->signatureMethod, $this->consumer, $this->token); $authHeader = $oauth->to_header(); $pieces = explode(' ', $authHeader, 2); $authString = $pieces[1]; if ($method === 'GET') { // GET requests get the params in the query string $request = $this->client->get($url, null); $request->addHeader('Authorization', $authString); $request->getQuery()->merge($options); } else { // POST requests get the params in the body, with the files added // and as multipart if appropriate $request = $this->client->post($url, null, $options); $request->addHeader('Authorization', $authString); if ($file) { if (is_array($file)) { $collection = array(); foreach ($file as $idx => $f) { $collection["data[{$idx}]"] = $f; } $request->addPostFiles($collection); } else { $request->addPostFiles(array('data' => $file)); } } } $request->setHeader('User-Agent', 'tumblr.php/' . $this->version); // Guzzle throws errors, but we collapse them and just grab the // response, since we deal with this at the \Tumblr\Client level try { $response = $request->send(); } catch (\Guzzle\Http\Exception\BadResponseException $e) { $response = $request->getResponse(); } // Construct the object that the Client expects to see, and return it $obj = new \stdClass(); $obj->status = $response->getStatusCode(); $obj->body = $response->getBody(); $obj->headers = $response->getHeaders()->toArray(); return $obj; }
public function request($method, $path, $options) { // Ensure we have options $options ?: array(); // Take off the data param, we'll add it back after signing $file = isset($options['data']) ? $options['data'] : false; unset($options['data']); // Check for a URL option, if it exists then we'll override $url = "http://api.tumblr.com/{$path}"; if (isset($options['url'])) { $url = "{$options['url']}/{$path}"; unset($options['url']); } $oauth = \Eher\OAuth\Request::from_consumer_and_token($this->consumer, $this->token, $method, $url, $options); $oauth->sign_request($this->signatureMethod, $this->consumer, $this->token); $authHeader = $oauth->to_header(); $pieces = explode(' ', $authHeader, 2); $authString = $pieces[1]; if ($method === 'GET') { // GET requests get the params in the query string $request = $this->client->get($url, null); $request->addHeader('Authorization', $authString); $request->getQuery()->merge($options); } else { // POST requests get the params in the body, with the files added // and as multipart if appropriate $request = $this->client->post($url, null, $options); $request->addHeader('Authorization', $authString); if ($file) { $request->addPostFiles(array('data' => $file)); } } // Guzzle throws errors, but we collapse them and just grab the // response, since we deal with this at the \Tumblr\Client level try { $response = $request->send(); } catch (\Guzzle\Http\Exception\BadResponseException $e) { $response = $request->getResponse(); } // Construct the object that the Client expects to see, and return it $obj = new \stdClass(); $obj->status = $response->getStatusCode(); $obj->body = $response->getBody(); $obj->headers = $response->getHeaders(); return $obj; }
/** * @param \Eher\OAuth\Token $requestToken * @param string $verifier * * @throws \Exception * @return \Eher\OAuth\Token */ public function getAccessToken(\Eher\OAuth\Token $requestToken, $verifier) { $this->logger->info("Obtaining Access Token", array("host" => $this->host, "verifier" => $verifier)); // create the URL for the Access Token Endpoint $url = sprintf(self::ACCESS_TOKEN_URL, $this->host); // create an OAuth Request- configure with local parameters $oAuthRequest = OAuthRequest::from_consumer_and_token($this->consumer, $requestToken, 'GET', $url); $oAuthRequest->set_parameter('oauth_verifier', $verifier); $oAuthRequest->sign_request($this->signatureMethod, $this->consumer, $requestToken); // Perform request & Retrieve result $headers = array($oAuthRequest->to_header(), 'Accept: application/x-www-form-urlencoded'); $resultString = $this->performRequest($url, 'GET', $headers); // parse the result string (should be www-form-urlencoded parse_str($resultString, $result); // detect problems if (isset($result['oauth_problem'])) { $this->logger->error("Failed to obtain Access Token", array("host" => $this->host, "verifier" => $verifier, "problem" => $result['oauth_problem'])); throw new \Exception("Failed to retrieve Access Token because: {$result['oauth_problem']}"); } if (!isset($result['oauth_token']) || !isset($result['oauth_token_secret'])) { $this->logger->error("Failed to obtain Access Token", array("host" => $this->host, "verifier" => $verifier)); throw new \Exception("Failed to retrieve Access Token."); } // set the access token for later use $this->accessToken = new OAuthToken($result['oauth_token'], $result['oauth_token_secret']); // return the token return $this->accessToken; }
/** * Obtains a user's login details from a token key and secret * * @param string $tokenKey The token key provided by VATSIM * @param secret $tokenSecret The secret associated with the token * @return object|false false if error, otherwise returns user details */ public function checkLogin($tokenKey, $tokenSecret, $tokenVerifier) { $this->token = new Consumer($tokenKey, $tokenSecret); // the location to send a cURL request to to obtain this user's details $returnUrl = $this->base . $this->loc_api . $this->loc_return . $this->format . '/'; // generate a token request call using post data $req = Request::from_consumer_and_token($this->consumer, $this->token, "POST", $returnUrl, array('oauth_token' => $tokenKey, 'oauth_verifier' => $tokenVerifier)); // sign the request using the specified signature/encryption method (set in this class) $req->sign_request($this->signature, $this->consumer, $this->token); // post the details to VATSIM and obtain the result $response = $this->curlRequest($returnUrl, $req->to_postdata()); if ($response) { // convert using our response format (depending upon user preference) $sso = $this->responseFormat($response); // did VATSIM return a successful result? if ($sso->request->result == 'success') { // one time use of tokens only, token no longer valid $this->token = false; // return the full object to the user return $sso; } else { // oauth returned a failed request, store the error details $this->error = array('type' => 'oauth_response', 'code' => false, 'message' => $sso->request->message); return false; } } else { // cURL response failed return false; } }
/** * Generate the HTTP headers need for a given Cloud API method. * * @param string $method API method * @param string $http_method Optional, HTTP request method * * @return array contains headers to use for HTTP requests */ public function getHeaders($method, $http_method = "POST") { $headers = array(); $consumer = new \Eher\OAuth\Consumer($this->signature['consumer_key'], $this->signature['shared_secret']); $signatureMethod = new \Eher\OAuth\HmacSha1(); $token = new \Eher\OAuth\Token($this->signature['oauth_token'], $this->signature['oauth_secret']); $request = \Eher\OAuth\Request::from_consumer_and_token($consumer, $token, $http_method, $this->api_url . "/" . $this->GetEndpoint($method), array()); $request->sign_request($signatureMethod, $consumer, $token); if ($method == "has_object_parts_v2" || $method == "send_object_parts_v2" || $method == "get_object_parts_v2") { array_push($headers, "Content-Type: application/octet-stream"); } array_push($headers, "X-Api-Version: 1.0"); array_push($headers, "X-Client-Type: api"); array_push($headers, "X-Client-Time: " . time()); array_push($headers, $request->to_header()); return $headers; }
function checkImportStatus($importId) { $url = sprintf('https://%s.cartodb.com/api/v1/imports/%s', $this->subdomain, $importId); $params = array(); $headers = array(); $headers['Accept'] = 'application/json'; if (!empty($this->apiKey)) { $params['api_key'] = $this->apiKey; $request = new Request('POST', $url, $params); } elseif (!empty($this->consumerKey) && !empty($this->consumerSecret)) { $sig_method = new HmacSha1(); $consumer = new Consumer($this->consumerKey, $this->consumerSecret, NULL); $token = $this->storage->getToken(); $request = Request::from_consumer_and_token($consumer, $token, 'GET', $url, $params); $request->sign_request($sig_method, $consumer, $token); } else { throw new \RuntimeException('Need at least one authentication method to access private tables'); } $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = array(); $response['return'] = $this->json_decode ? (array) json_decode(curl_exec($ch)) : curl_exec($ch); $response['info'] = curl_getinfo($ch); curl_close($ch); if ($response['info']['http_code'] == 401) { $this->authorized = $this->getAccessToken(); return $this->request($url, 'POST', $params); } $payload = new Payload($request); $payload->setRawResponse($response); return $payload; }