예제 #1
0
파일: API.php 프로젝트: Geotab/mygeotab-php
 /**
  * @param $method
  * @param array $post
  * @param null $successCallback
  * @param null $errorCallback
  */
 private function request($method, array $post, $successCallback, $errorCallback)
 {
     $url = "https://" . $this->credentials->getServer() . "/apiv1";
     $postData = "JSON-RPC=" . urlencode(json_encode(["method" => $method, "params" => $post]));
     $headers = ["Connection: keep-alive", "Content-Type: application/x-www-form-urlencoded", "Charset=UTF-8", "Cache-Control: no-cache", "Pragma: no-cache"];
     $defaults = array(CURLOPT_URL => $url, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $postData, CURLOPT_HTTPHEADER => $headers, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_TIMEOUT => 30, CURLOPT_ENCODING => "gzip", CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSLVERSION => 6);
     $ch = curl_init();
     curl_setopt_array($ch, $defaults);
     $response = curl_exec($ch);
     $error = curl_error($ch);
     if ($error != "") {
         trigger_error(curl_error($ch));
     }
     $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
     //$headerReturn = substr($response, 0, $header_size);
     $body = substr($response, $header_size);
     curl_close($ch);
     $result = json_decode($body, true);
     // If callbacks are specified - then call them. Otherwise, just return the results or throw an error
     if ($this->array_check("result", $result)) {
         if (is_callable($successCallback)) {
             $successCallback($result["result"]);
         } else {
             return $result["result"];
         }
     } else {
         if (count($result) == 0) {
             if (is_callable($successCallback)) {
                 $successCallback($result);
             } else {
                 return $result;
             }
         } else {
             if (is_callable($errorCallback)) {
                 $errorCallback($result["error"]["errors"][0]);
             } else {
                 throw new MyGeotabException($result["error"]["errors"][0]);
             }
         }
     }
 }