/** * @param string $url * @param array $data * @param array $curlOptions * * @return bool|mixed|\stdClass */ protected function postData($url, $data, $curlOptions = []) { if (false === $this->checkPayload($data)) { return false; } try { if (false === ($_result = Curl::post($url, $data, $curlOptions))) { \Log::error('[dfe.license] Network communication error.'); return false; } // Yay? if (Response::HTTP_OK == Curl::getLastHttpCode()) { return $_result; } \Log::error('[dfe.license] POST failed with status "' . Curl::getLastHttpCode() . '". Response: ' . print_r($_result, true)); } catch (\Exception $_ex) { \Log::error('[dfe.license] Exception while POSTing: ' . $_ex->getMessage()); } return false; }
/** * @param string $method * @param string $url * @param mixed $payload * @param array $curlOptions * * @return \DreamFactory\Core\Utility\ServiceResponse * @throws \DreamFactory\Core\Exceptions\NotImplementedException * @throws \DreamFactory\Core\Exceptions\RestException */ protected static function externalRequest($method, $url, $payload = [], $curlOptions = []) { $result = Curl::request($method, $url, $payload, $curlOptions); $contentType = Curl::getInfo('content_type'); $format = DataFormats::fromMimeType($contentType); $status = Curl::getLastHttpCode(); if ($status >= 300) { if (!is_string($result)) { $result = json_encode($result); } throw new RestException($status, $result, $status); } return ResponseFactory::create($result, $format, $status, $contentType); }
/** * @throws \DreamFactory\Core\Exceptions\RestException * @return bool */ protected function processRequest() { $data = $this->request->getContent(); $resource = !empty($this->resourcePath) ? ltrim($this->resourcePath, '/') : null; if ($resource) { $this->url = rtrim($this->baseUrl, '/') . '/' . $resource; } else { $this->url = $this->baseUrl; } if (!empty($this->query)) { $splicer = false === strpos($this->baseUrl, '?') ? '?' : '&'; $this->url .= $splicer . $this->query; } $cacheKey = ''; if ($this->cacheEnabled) { switch ($this->action) { case Verbs::GET: // build cache_key $cacheKey = $this->action . ':' . $this->name; if ($resource) { $cacheKey .= ':' . $resource; } if (!empty($this->cacheQuery)) { $cacheKey .= ':' . $this->cacheQuery; } $cacheKey = hash('sha256', $cacheKey); if (null !== ($result = $this->getFromCache($cacheKey))) { return $result; } break; } } Log::debug('Outbound HTTP request: ' . $this->action . ': ' . $this->url); Curl::setDecodeToArray(true); $result = Curl::request($this->action, $this->url, $data, $this->curlOptions); if (false === $result) { $error = Curl::getError(); throw new RestException(ArrayUtils::get($error, 'code', 500), ArrayUtils::get($error, 'message')); } $status = Curl::getLastHttpCode(); if ($status >= 300) { if (!is_string($result)) { $result = json_encode($result); } throw new RestException($status, $result, $status); } $contentType = Curl::getInfo('content_type'); $format = DataFormats::fromMimeType($contentType); $response = ResponseFactory::create($result, $format, $status, $contentType); if ($this->cacheEnabled) { switch ($this->action) { case Verbs::GET: $this->addToCache($cacheKey, $result); break; } } return $response; }