/**
* Executes an HTTP request
*
* @param string $data query string OR POST content as a string
* @return mixed
* @throws PayPalConnectionException
*/
public function execute($data)
{
//Initialize the logger
$this->logger->info($this->httpConfig->getMethod() . ' ' . $this->httpConfig->getUrl());
//Initialize Curl Options
$ch = curl_init($this->httpConfig->getUrl());
curl_setopt_array($ch, $this->httpConfig->getCurlOptions());
curl_setopt($ch, CURLOPT_URL, $this->httpConfig->getUrl());
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHttpHeaders());
//Determine Curl Options based on Method
switch ($this->httpConfig->getMethod()) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, true);
case 'PUT':
case 'PATCH':
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
}
//Default Option if Method not of given types in switch case
if ($this->httpConfig->getMethod() != NULL) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->httpConfig->getMethod());
}
//Logging Each Headers for debugging purposes
foreach ($this->getHttpHeaders() as $header) {
//TODO: Strip out credentials and other secure info when logging.
$this->logger->fine($header);
}
$this->logger->fine(($data && $data != '' ? "Payload : " . $data : "No Request Payload") . "\n");
//Execute Curl Request
$result = curl_exec($ch);
//Retry if Certificate Exception
if (curl_errno($ch) == 60) {
$this->logger->info("Invalid or no certificate authority found - Retrying using bundled CA certs file");
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
$result = curl_exec($ch);
}
//Retrieve Response Status
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//Retry if Failing
$retries = 0;
if (in_array($httpStatus, self::$retryCodes) && $this->httpConfig->getHttpRetryCount() != null) {
$this->logger->info("Got {$httpStatus} response from server. Retrying");
do {
$result = curl_exec($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
} while (in_array($httpStatus, self::$retryCodes) && ++$retries < $this->httpConfig->getHttpRetryCount());
}
//Throw Exception if Retries and Certificates doenst work
if (curl_errno($ch)) {
$ex = new PayPalConnectionException($this->httpConfig->getUrl(), curl_error($ch), curl_errno($ch));
curl_close($ch);
throw $ex;
}
//Close the curl request
curl_close($ch);
$this->logger->fine(($result && $result != '' ? "Response : " . $result : "No Response Body") . "\n\n");
//More Exceptions based on HttpStatus Code
if (in_array($httpStatus, self::$retryCodes)) {
$ex = new PayPalConnectionException($this->httpConfig->getUrl(), "Got Http response code {$httpStatus} when accessing {$this->httpConfig->getUrl()}. " . "Retried {$retries} times.");
$ex->setData($result);
throw $ex;
} else {
if ($httpStatus < 200 || $httpStatus >= 300) {
$ex = new PayPalConnectionException($this->httpConfig->getUrl(), "Got Http response code {$httpStatus} when accessing {$this->httpConfig->getUrl()}.", $httpStatus);
$ex->setData($result);
throw $ex;
}
}
//Return result object
return $result;
}