/** * * Make a cURL call * @param string $endpoint URL to call * @param string $method HTTP Method used for the call * @param string/Array $payload Body message to send with the call * @throws Exception * @return RestResponse */ public function makeCall($path, $method = "GET", $payload = "") { try { if (strpos($path, "/") !== 0) { $path = "/" . $path; } $endpoint = $this->protocol . "://" . $this->host . ":" . $this->port . $path; // Initialize the curl_session with targeted URL $ch = curl_init($endpoint); // Throw exception if curl is not initialize if ($ch == FALSE) { throw new CURLException("CURL Not available"); } // Set the content type depending the payload type if (is_array($payload)) { $contentType = "multipart/form-data"; } else { $contentType = "application/xml; charset=UTF-8"; } // Set global headers for the call $headers = array('Cache-Control: nocache', 'Accept: application/xml', 'Content-Type: ' . $contentType, 'Connection: Keep-Alive'); if (!empty($this->basicauth)) { $arr = explode(":", $this->basicauth, 2); $username = $arr[0]; $password = $arr[1]; $headers[] = $this->genAuthHeaderBasic($username, $password); } if (!empty($this->peerCertificatePath)) { // This will probably work, but I won't know until it's tested. // Until it's reliably tested, skip execution with an exception. throw new Exception("Not tested!"); curl_setopt($ch, CURLOPT_CAINFO, $this->{$peerCertificatePath}); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); } // Set cURL options for the call curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::CONNECT_TIMEOUT); // Execute the call $return = curl_exec($ch); // Close cURL session curl_close($ch); // Check if endpoint is unavailable if ($return == FALSE) { $err = "No Server Responding"; LoggerInterface::log($err, LoggerInterface::ERROR); throw new CURLException($err); } // Implements a new RestResponse to store useful informations $response = new RestResponse(); // Retrieve useful informations from REST call response // Extract the HTTP Code $response->setHTTPCode($this->getHTTPCode($return)); // Extract the specific X-E3-Application-Error-Code used for functionnal errors $response->setXApplicationCode($this->getSpecificApplicationCode($return)); // Extract the payload $response->setPayload($this->getBody($return)); // Throw Exception if BackEnd retrieve functionnal error if ($response->getXApplicationCode() !== NULL && $response->getXApplicationCode() != 200) { $errorMessage = "E3_API_Err_" . $response->getXApplicationCode(); throw new RESTAppException($errorMessage, $response->getXApplicationCode()); } // Throw Exception if BackEnd retrieve HTTP error if (false && !in_array($response->getHTTPCode(), array(200, 201, 202, 203, 204))) { if ($response->getHTTPCode() == 404) { $errorMessage = '404 - Server Unreachable'; } else { $errorXML = simplexml_load_string($response->getPayload()); if ($errorXML->status == "SUCCESS") { LoggerInterface::log("Response successful with improper return code ({$response->getHTTPCode()}): " . print_r($response->getPayload(), true), LoggerInterface::WARN); return $response; } LoggerInterface::log("Errored response: ({$response->getHTTPCode()}) " . print_r($response->getPayload(), true), LoggerInterface::DEBUG); $errorMessage = $errorXML->error->errorText; //$errorMessage = htmlspecialchars($response->getPayload()); } throw new Exception($errorMessage, $response->getHTTPCode()); } // Return the formatted response object return $response; } catch (Exception $e) { LoggerInterface::log('Rest Curl Call', $e->getMessage(), array(), LoggerInterface::ERROR); //drupal_set_message('An error as occured during the operation, please retry or contact an administrator.', 'error'); throw new Exception($e->getMessage()); } }