/** * Performs raw REST call * * @param ServiceRequestInterface $request * @return mixed * @throws CouldNotConnectException */ protected function callRaw(ServiceRequestInterface $request) { $url = rtrim($request->getLocation(), '/') . '/' . $request->getMethod(); $curl = curl_init(); if ($curl === false) { throw new CouldNotConnectException('cURL could not be initialized'); } $credentials = $request->getCredentials(); if ($this->basicAuth && !empty($credentials['name']) && !empty($credentials['password'])) { curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, $credentials['name'] . ":" . $credentials['password']); } $headers = $request->getHeaders(); switch ($this->method) { case RestService::METHOD_PATCH: case RestService::METHOD_POST: case RestService::METHOD_PUT: curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($request->getBody() ?: [])); $parameters = $request->getParameters(); if (!empty($parameters)) { $url .= '?' . http_build_query($request->getParameters()); } break; case RestService::METHOD_GET: $parameters = $request->getbody(); $url .= '?' . http_build_query($parameters ?: []); break; // default omitted on purpose } if (count($headers)) { curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_USERAGENT, self::USER_AGENT); curl_setopt($curl, CURLOPT_URL, $url); $response = curl_exec($curl); if ($response === false) { throw new CouldNotConnectException(curl_error($curl), curl_errno($curl)); } $this->responseInformation->setStatusCode(curl_getinfo($curl, CURLINFO_HTTP_CODE)); curl_close($curl); event(new RestCallCompleted($url, isset($parameters) ? $parameters : [], $this->sendResponseToEvent ? $response : null)); return $response; }
/** * Takes the current request and supplements it with the service's defaults * to merge them into a complete request. */ protected function supplementRequestWithDefaults() { // properties to set only if they are empty if (empty($this->request->getLocation())) { $this->request->setLocation($this->defaults->getLocation()); } if (empty($this->request->getMethod())) { $this->request->setMethod($this->defaults->getMethod()); } if (empty($this->request->getParameters())) { $this->request->setParameters($this->defaults->getParameters()); } if (empty($this->request->getBody())) { $this->request->setBody($this->defaults->getBody()); } if ($this->credentialsAreEmpty($this->request->getCredentials()) && !$this->credentialsAreEmpty($this->defaults->getCredentials())) { $this->request->setCredentials($this->defaults->getCredentials()['name'], $this->defaults->getCredentials()['password'], $this->defaults->getCredentials()['domain']); } // properties to expand if (!empty($this->defaults->getHeaders())) { $this->request->setHeaders(array_merge($this->request->getHeaders(), $this->defaults->getHeaders())); } }