Пример #1
0
 public function getResponse()
 {
     // Set Parameters
     if (sizeof($this->parameters) > 0) {
         $query = substr($this->uri, -1) !== '?' ? '?' : '&';
         foreach ($this->parameters as $var => $value) {
             if ($value == null || $value == '') {
                 $query .= $var . '&';
             } else {
                 $query .= $var . '=' . rawurlencode($value) . '&';
             }
         }
         $query = substr($query, 0, -1);
         $this->uri .= $query;
     }
     if ($this->queryString != null) {
         $this->uri .= substr($this->queryString, 0, 1) !== "&" ? '&' : '';
         $this->uri .= $this->queryString;
     }
     // Set Url
     $url = $this->endpoint . '/' . $this->version . '/' . $this->uri;
     try {
         if (BigML::getDebug() != null && BigML::getDebug() == true) {
             echo "URL: " . $url . "\n";
         }
         $curl = curl_init();
         curl_setopt($curl, CURLOPT_URL, $url);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($curl, CURLOPT_HEADER, true);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
         if ($this->method == "CREATE") {
             curl_setopt($curl, CURLOPT_POST, true);
             curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
         } elseif ($this->method == "UPDATE") {
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
             curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
         } elseif ($this->method == "DELETE") {
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
         } elseif ($this->method == "DOWNLOAD") {
             curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
             curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
             curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
             curl_setopt($curl, CURLOPT_FILE, $this->data);
         }
         // Set Headers
         $headers = array();
         foreach ($this->headers as $header => $value) {
             if (strlen($value) > 0) {
                 $headers[] = $header . ': ' . $value;
             }
         }
         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
         if (BigML::getDebug() != null && BigML::getDebug() == true) {
             curl_setopt($curl, CURLOPT_VERBOSE, true);
         }
         $response = curl_exec($curl);
         $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
         if ($code == $this->response_code) {
             $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
             $header = substr($response, 0, $header_size);
             $body = substr($response, $header_size);
             $this->parseJsonResponse($body, $code, $header);
         } else {
             if (in_array(intval($code), array(BigMLRequest::HTTP_BAD_REQUEST, BigMLRequest::HTTP_UNAUTHORIZED, BigMLRequest::HTTP_NOT_FOUND, BigMLRequest::HTTP_TOO_MANY_REQUESTS))) {
                 $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
                 $header = substr($response, 0, $header_size);
                 $body = substr($response, $header_size);
                 $this->response["code"] = $code;
                 $error_message = $this->error_message(json_decode($body), $this->method);
                 $this->response["error"]["status"]["message"] = $error_message["message"];
                 $this->response["error"]["status"]["code"] = $code;
                 error_log($this->response["error"]["status"]["message"]);
                 if ($error_message["code"] != null) {
                     $this->response["error"]["status"]["code"] = $error_message["code"];
                 }
             } else {
                 error_log("Unexpected error " . $code);
                 $this->response["code"] = BigMLRequest::HTTP_INTERNAL_SERVER_ERROR;
             }
         }
         curl_close($curl);
     } catch (Exception $e) {
         error_log("Unexpected exception error");
     }
     return json_decode(json_encode($this->response));
 }