public function saveFile($file_path_name, $file_content, $overwrite = false) { $target = Backendless::getUrl() . "/" . Backendless::getApplicationId() . "/" . Backendless::getVersion() . "/files/binary/" . $file_path_name; if ($overwrite) { $target .= "?overwrite=true"; } if (is_array($file_content)) { $file_content = implode($file_content); } $file_content = base64_encode($file_content); $http_request = new HttpRequest(); $multipart_boundary = "------BackendlessFormBoundary" . md5(uniqid()) . microtime(true); $content = "--" . $multipart_boundary . "\r\n" . "Content-Disposition: form-data; name=\"model-file\"; filename=\"" . basename($file_path_name) . "\"\r\n" . "Content-Type: text/plain\r\n\r\n" . $file_content . "\r\n"; $content .= "--" . $multipart_boundary . "--\r\n"; RequestBuilder::addUserTokenHeader($http_request); $http_request->setTargetUrl($target)->setHeader(self::$APP_ID_KEY, Backendless::getApplicationId())->setHeader(self::$SECRET_KEY, Backendless::getSecretKey())->setHeader(self::$VERSION, Backendless::getVersion())->setHeader('Content-type', ' multipart/form-data; boundary=' . $multipart_boundary)->setHeader("application-type:", "REST")->request($content); if ($http_request->getResponseCode() != 200) { $error = json_decode($http_request->getResponse(), true); if (!isset($error['message'])) { throw new Exception("API responce " . $http_request->getResponseStatus() . ' ' . $http_request->getResponseCode() . $http_request->getResponse()); } else { throw new Exception($error['message'], $error['code']); } } return json_decode($http_request->getResponse(), true); }
public static function Get($url) { $http_request = new HttpRequest(); $http_request->setTargetUrl($url)->setHeader('application-id', Backendless::getApplicationId())->setHeader('secret-key', Backendless::getSecretKey())->setHeader('application-type', 'REST')->setHeader('Accept:', '*/*')->setHeader('Content-Type', 'application/json'); foreach (self::$headers as $heder_n => $h_val) { $http_request->setHeader($heder_n, $h_val); } self::addUserTokenHeader($http_request); self::$headers = []; $http_request->request('', 'GET'); self::handleError($http_request); return $http_request->getResponse(); }
private function runAction($action_name, $geofence_name, $geopoint = null) { $url = "fence/" . $action_name . "?geoFence=" . $geofence_name; $headers = []; $headers['application-id'] = Backendless::getApplicationId(); $headers['secret-key'] = Backendless::getSecretKey(); $headers['application-type'] = 'REST'; $geopoint_data = []; if ($geopoint !== null) { if (is_object($geopoint)) { $geopoint_data['latitude'] = $geopoint->getLatitude(); $geopoint_data['longitude'] = $geopoint->getLongitude(); } else { $geopoint_data = $geopoint; } $headers['Content-Type'] = 'application/json'; } else { $geopoint_data = null; } return RequestBuilder::doRequestWithHeaders("geo", $url, $geopoint_data, "POST", $headers)["totalObjects"]; }