/** * @param HttpRequest $httpRequest * @param boolean $onlyAllowHttps * * @return HttpResponse */ public function execute($httpRequest, $onlyAllowHttps = true) { $url = $httpRequest->getUrl(); $post = $httpRequest->getMethod() == HttpRequest::METH_POST ? 1 : 0; $xml = $httpRequest->getBody(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, $post); curl_setopt($ch, CURLOPT_USERAGENT, "realex sdk version 0.1"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain')); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout); curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->socketTimeout); if ($onlyAllowHttps === false) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); } $responseXml = curl_exec($ch); $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $errorNumber = curl_errno($ch); if ($errorNumber) { $this->logger->error("Exception communicating with Realex. Error number: " . $errorNumber . ". Description: " . curl_error($ch)); curl_close($ch); throw new RealexException("Exception communicating with Realex"); } curl_close($ch); $response = new HttpResponse(); $response->setResponseCode($statusCode); $response->setBody($responseXml); return $response; }
/** * * Perform the actual send of the message, according to the HttpConfiguration, and get the response. * This will also check if only HTTPS is allowed, based on the {@link HttpConfiguration}, and will * throw a {@link RealexException} if HTTP is used when only HTTPS is allowed. A {@link RealexException} * is also thrown if the response from Realex is not success (ie. if it's not 200 status code). * * @param string $xml * @param HttpClient $httpClient * @param HttpConfiguration $httpConfiguration * * * @return string */ public static function sendMessage($xml, HttpClient $httpClient, HttpConfiguration $httpConfiguration) { self::getLogger(); self::$logger->debug("Setting endpoint of: " . $httpConfiguration->getEndpoint()); $httpPost = new HttpRequest($httpConfiguration->getEndpoint(), HttpRequest::METH_POST); $response = null; // Confirm protocol is HTTPS (ie. secure) if such is configured if ($httpConfiguration->isOnlyAllowHttps()) { $scheme = parse_url($httpPost->getUrl(), PHP_URL_SCHEME); if (!$scheme || strtolower($scheme) != strtolower(self::HTTPS_PROTOCOL)) { self::$logger->error("Protocol must be " . self::HTTPS_PROTOCOL); throw new RealexException("Protocol must be " . self::HTTPS_PROTOCOL); } } else { self::$logger->warn("Allowed send message over HTTP. This should NEVER be allowed in a production environment."); } try { self::$logger->debug("Setting entity in POST message."); $httpPost->setBody($xml); self::$logger->debug("Executing HTTP Post message to: " . $httpPost->getUrl()); $response = $httpClient->execute($httpPost, $httpConfiguration->isOnlyAllowHttps()); self::$logger->debug("Checking the HTTP response status code."); $statusCode = $response->getResponseCode(); if ($statusCode != 200) { if ($statusCode == 404) { throw new RealexException("Exception communicating with Realex"); } else { throw new RealexException("Unexpected http status code [" . $statusCode . "]"); } } self::$logger->debug("Converting HTTP entity (the xml response) back into a string."); $xmlResponse = $response->getBody(); return $xmlResponse; } catch (RealexException $e) { throw $e; } catch (Exception $e) { self::$logger->error("Exception communicating with Realex.", $e->getMessage()); throw new RealexException("Exception communicating with Realex", $e); } }