/** * Compropago Client Constructor * @param array $params * @throws Exception */ public function __construct($params = array()) { if (!array_key_exists('publickey', $params) || !array_key_exists('privatekey', $params) || empty($params['publickey']) || empty($params['privatekey'])) { $error = "Se requieren las llaves del API de Compropago"; throw new Exception($error); } else { $this->auth = [$params['privatekey'], $params['publickey']]; ///////////cambiar a formato curl? //Modo Activo o Pruebas if ($params['live'] == false) { $this->deployUri = self::API_SANDBOX_URI; $this->deployMode = true; } else { $this->deployUri = self::API_LIVE_URI; $this->deployMode = false; } if (isset($params['contained']) && !empty($params['contained'])) { $extra = $params['contained']; } else { $extra = 'SDK; PHP ' . phpversion() . ';'; } $http = new Request($this->deployUri); $http->setUserAgent(self::USER_AGENT_SUFFIX, $this->getVersion(), $extra); $http->setAuth($this->auth); $this->http = $http; } }
/** * @param Compropago\Request $request objeto con los parametros validados de una petición * @return array ASSOC responseBody responseHeaders responseCode */ public function executeRequest(Request $request) { $curl = curl_init(); if ($request->getData()) { curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getData()); } $requestHeaders = $request->getRequestHeaders(); if ($requestHeaders && is_array($requestHeaders)) { $curlHeaders = array(); foreach ($requestHeaders as $k => $v) { $curlHeaders[] = "{$k}: {$v}"; } curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders); } curl_setopt($curl, CURLOPT_URL, $request->getServiceUrl()); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod()); curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent()); curl_setopt($curl, CURLOPT_USERPWD, $request->getAuth()); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); if (function_exists('curl_setopt_array')) { curl_setopt_array($curl, $request->getOptions()); } else { //throws Warning foreach ($request->getOptions() as $key => $var) { curl_setopt($curl, $key, $var); } } $response = curl_exec($curl); if ($response === false) { $error = curl_error($curl); $code = curl_errno($curl); if ($code == 60 || $code == 77) { curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); $response = curl_exec($curl); } if ($response == false) { $error = curl_error($curl); $code = curl_errno($curl); throw new Exception($error, $code); } } $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize); $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); return array('responseBody' => $responseBody, 'responseHeaders' => $responseHeaders, 'responseCode' => $responseCode); }