Example #1
0
 public function _doUrlRequest($httpVerb, $url, $requestBody = null)
 {
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_TIMEOUT, 60);
     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $httpVerb);
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
     $headers = $this->_getHeaders($curl);
     $headers[] = 'User-Agent: Braintree PHP Library ' . Version::get();
     $headers[] = 'X-ApiVersion: ' . Configuration::API_VERSION;
     $authorization = $this->_getAuthorization();
     if (isset($authorization['user'])) {
         curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
         curl_setopt($curl, CURLOPT_USERPWD, $authorization['user'] . ':' . $authorization['password']);
     } else {
         if (isset($authorization['token'])) {
             $headers[] = 'Authorization: Bearer ' . $authorization['token'];
         }
     }
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     // curl_setopt($curl, CURLOPT_VERBOSE, true);
     if ($this->_config->sslOn()) {
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
         curl_setopt($curl, CURLOPT_CAINFO, $this->getCaFile());
     }
     if (!empty($requestBody)) {
         curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
     }
     if ($this->_config->isUsingProxy()) {
         $proxyHost = $this->_config->getProxyHost();
         $proxyPort = $this->_config->getProxyPort();
         $proxyType = $this->_config->getProxyType();
         curl_setopt($curl, CURLOPT_PROXY, $proxyHost . ':' . $proxyPort);
         if (!empty($proxyType)) {
             curl_setopt($curl, CURLOPT_PROXYTYPE, $proxyType);
         }
     }
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     if (isset($this->_logger)) {
         $this->_logger->setRequest($requestBody, $url);
         $this->_logger->setRequestTime(time());
     }
     $response = curl_exec($curl);
     $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     if (isset($this->_logger)) {
         $this->_logger->setResponse($response, $httpStatus);
         $this->_logger->setResponseTime(time());
     }
     curl_close($curl);
     if ($this->_config->sslOn()) {
         if ($httpStatus == 0) {
             throw new Exception\SSLCertificate();
         }
     }
     return ['status' => $httpStatus, 'body' => $response];
 }