示例#1
0
文件: TMDb.php 项目: kaibosh/nZEDb
 /**
  * Makes the call to the API
  *
  * @param string $function			API specific function name for in the URL
  * @param array $params				Unencoded parameters for in the URL
  * @param string $session_id		Session_id for authentication to the API for specific API methods
  * @param const $method				TMDb::GET or TMDb:POST (default TMDb::GET)
  * @return TMDb result array
  */
 private function _makeCall($function, $params = NULL, $session_id = NULL, $method = TMDb::GET)
 {
     $params = !is_array($params) ? array() : $params;
     $auth_array = array('api_key' => $this->_apikey);
     if ($session_id !== NULL) {
         $auth_array['session_id'] = $session_id;
     }
     $url = $this->_apischeme . TMDb::API_URL . '/' . TMDb::API_VERSION . '/' . $function . '?' . http_build_query($auth_array, '', '&');
     if ($method === TMDb::GET) {
         if (isset($params['language']) and $params['language'] === FALSE) {
             unset($params['language']);
         }
         if (isset($params['include_adult'])) {
             $params['include_adult'] = $params['include_adult'] ? 'true' : 'false';
         }
         $url .= !empty($params) ? '&' . http_build_query($params, '', '&') : '';
     }
     $results = '{}';
     if (extension_loaded('curl')) {
         $headers = array('Accept: application/json');
         $ch = curl_init();
         if ($method == TMDB::POST) {
             $json_string = json_encode($params);
             curl_setopt($ch, CURLOPT_POST, 1);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $json_string);
             $headers[] = 'Content-Type: application/json';
             $headers[] = 'Content-Length: ' . strlen($json_string);
         } elseif ($method == TMDb::HEAD) {
             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
             curl_setopt($ch, CURLOPT_NOBODY, 1);
         }
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
         curl_setopt($ch, CURLOPT_HEADER, 1);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         $this->incRequests();
         curl_setopt_array($ch, nzedb\utility\Misc::curlSslContextOptions());
         $response = curl_exec($ch);
         $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
         $header = substr($response, 0, $header_size);
         $body = substr($response, $header_size);
         $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
         $error_number = curl_errno($ch);
         $error_message = curl_error($ch);
         // If temp banned, you need to wait for 10 sec
         if ($http_status == 503 or $http_status == 0) {
             if ($this->_retries < 3) {
                 $this->_retries += 1;
                 echo "\nTMDB limits exceeded, sleeping for 10 seconds.";
                 usleep(10 * 1000 * 1000);
                 return $this->_makeCall($function, $params, $session_id, $method);
             }
         }
         $this->_retries = 0;
         if ($error_number > 0) {
             throw new TMDbException('Method failed: ' . $function . ' - HTTP Status ' . $http_status . ' Curl Errno ' . $error_number . ' Curl Error ' . $error_message);
         }
         curl_close($ch);
     } else {
         throw new TMDbException('CURL-extension not loaded');
     }
     $results = json_decode($body, TRUE);
     if (strpos($function, 'authentication/token/new') !== FALSE) {
         $parsed_headers = $this->_http_parse_headers($header);
         $results['Authentication-Callback'] = $parsed_headers['Authentication-Callback'];
     }
     if ($results !== NULL) {
         return $results;
     } elseif ($method == TMDb::HEAD) {
         return $this->_http_parse_headers($header);
     } else {
         throw new TMDbException('Server error on "' . $url . '": ' . $response);
     }
 }
示例#2
0
 /**
  * Send call to API
  *
  * @param string $module string name of url suffix
  * @param array $params array get parameters to send to API
  *
  * @return mixed|array response of API
  * @throws GiantBombException
  */
 private function call($module, $params = array())
 {
     // set api data
     $params['api_key'] = $this->api_key;
     $params['format'] = $this->resp_type;
     // build URL
     $url = $this->endpoint . $module . '?' . http_build_query($params);
     // Set URL
     curl_setopt($this->ch, CURLOPT_URL, $url);
     curl_setopt_array($this->ch, nzedb\utility\Misc::curlSslContextOptions());
     // Send the request & save response to $resp
     $resp["data"] = curl_exec($this->ch);
     if (curl_errno($this->ch)) {
         throw new \GiantBombException('API call failed: ' . curl_error($this->ch));
     }
     // save http response code
     $resp["httpCode"] = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
     if (!$resp || !$resp["data"]) {
         throw new \GiantBombException("Couldn't get information from API");
     }
     return $resp;
 }
示例#3
0
 /**
  * @param        $region
  * @param        $params
  * @param        $public_key
  * @param        $private_key
  * @param string $associate_tag
  *
  * @return bool|SimpleXMLElement|string
  */
 private function aws_signed_request($region, $params, $public_key, $private_key, $associate_tag = "")
 {
     if ($public_key !== "" && $private_key !== "" && $associate_tag !== "") {
         $method = "GET";
         // Must be in small case.
         $host = "ecs.amazonaws." . $region;
         $uri = "/onca/xml";
         $params["Service"] = "AWSECommerceService";
         $params["AWSAccessKeyId"] = $public_key;
         $params["AssociateTag"] = $associate_tag;
         $params["Timestamp"] = gmdate("Y-m-d\\TH:i:s\\Z");
         $params["Version"] = "2009-03-31";
         /* The params need to be sorted by the key, as Amazon does this at
         			their end and then generates the hash of the same. If the params
         			are not in order then the generated hash will be different thus
         			failing the authetication process.
         			*/
         ksort($params);
         $canonicalized_query = array();
         foreach ($params as $param => $value) {
             $param = str_replace("%7E", "~", rawurlencode($param));
             $value = str_replace("%7E", "~", rawurlencode($value));
             $canonicalized_query[] = $param . "=" . $value;
         }
         $canonicalized_query = implode("&", $canonicalized_query);
         $string_to_sign = $method . "\n" . $host . "\n" . $uri . "\n" . $canonicalized_query;
         /* Calculate the signature using HMAC with SHA256 and base64-encoding.
          * The 'hash_hmac' function is only available from PHP 5 >= 5.1.2.
          */
         $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
         // Encode the signature for the request.
         $signature = str_replace("%7E", "~", rawurlencode($signature));
         // Create request.
         $request = "http://" . $host . $uri . "?" . $canonicalized_query . "&Signature=" . $signature;
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $request);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
         curl_setopt_array($ch, nzedb\utility\Misc::curlSslContextOptions());
         $xml_response = curl_exec($ch);
         if ($xml_response === False) {
             return False;
         } else {
             // Parse XML.
             $parsed_xml = @simplexml_load_string($xml_response);
             return $parsed_xml === False ? False : $parsed_xml;
         }
     } else {
         return "missingkey";
     }
 }