/** * * creates a curl resource with the parameters provided by the {@link Request} object * * @param Request $request * @return curl resource */ private function getCurlInstance(Request $request) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $request->buildUrl(true, $request->getMethod() == 'GET')); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Accept-Charset: utf-8')); if ($request->getUsername() && $request->getPassword()) { curl_setopt($ch, CURLOPT_USERPWD, $request->getUsername() . ':' . $request->getPassword()); } return $ch; }
/** * * @param \OptimalPayments\Request $request * @return type * @throws NetbanxException * @throws \OptimalPayments\NetbanxException */ public function processRequest(Request $request) { $curl = curl_init(); $opts = array(CURLOPT_URL => $request->buildUrl($this->apiEndPoint), CURLOPT_HTTPHEADER => array('Authorization: Basic ' . base64_encode($this->keyID . ':' . $this->keyPassword), 'Content-Type: application/json; charset=utf-8'), CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2); if ($cert = static::getCACertPath()) { $opts[CURLOPT_CAINFO] = $cert; } elseif ($cert = getenv('SSL_CERT_FILE')) { $opts[CURLOPT_CAINFO] = $cert; } if ($request->method != Request::GET) { $jsonData = $request->body ? $request->body->toJson() : ""; $opts[CURLOPT_CUSTOMREQUEST] = $request->method; $opts[CURLOPT_POSTFIELDS] = $jsonData; $opts[CURLOPT_HTTPHEADER][] = 'Content-Length: ' . strlen($jsonData); } curl_setopt_array($curl, $opts); $response = curl_exec($curl); if ($response === false) { throw $this->getNetBanxException(null, 'cURL has encountered an error in connecting to the host: (' . curl_errno($curl) . ') ' . curl_error($curl) . '. See cURL error codes for explanations: http://curl.haxx.se/libcurl/c/libcurl-errors.html', curl_errno($curl)); } $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if (!($return = json_decode($response, true))) { if ($responseCode < 200 || $responseCode >= 206) { throw $this->getNetbanxException($responseCode); } return true; } if (is_array($return)) { if ($responseCode < 200 || $responseCode >= 206) { $error = $this->getNetbanxException($responseCode, $return['error']['message'], $return['error']['code']); $error->rawResponse = $return; if (array_key_exists('error', $return)) { if (array_key_exists('fieldErrors', $return['error'])) { $error->fieldErrors = $return['error']['fieldErrors']; } if (array_key_exists('links', $return['error'])) { $error->links = $return['error']['links']; } if (array_key_exists('details', $return['error'])) { $error->details = $return['error']['details']; } } throw $error; } return $return; } else { throw $this->getNetbanxException($responseCode, $return); } }