PayPal\Core\PPHttpConnection::execute PHP Method

execute() public method

Executes an HTTP request
public execute ( string $data )
$data string query string OR POST content as a string
    public function execute($data)
    {
        $this->logger->fine("Connecting to " . $this->httpConfig->getUrl());
        $this->logger->fine("Payload " . $data);
        $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());
        switch ($this->httpConfig->getMethod()) {
            case 'POST':
                curl_setopt($ch, CURLOPT_POST, true);
            case 'PUT':
            case 'PATCH':
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                break;
        }
        if ($this->httpConfig->getMethod() != null) {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->httpConfig->getMethod());
        }
        foreach ($this->getHttpHeaders() as $header) {
            //TODO: Strip out credentials and other secure info when logging.
            $this->logger->info("Adding header {$header}");
        }
        $result = curl_exec($ch);
        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);
        }
        $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $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());
        }
        if (curl_errno($ch)) {
            $ex = new PPConnectionException($this->httpConfig->getUrl(), curl_error($ch), curl_errno($ch));
            curl_close($ch);
            throw $ex;
        }
        curl_close($ch);
        if (in_array($httpStatus, self::$retryCodes)) {
            $ex = new PPConnectionException($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 PPConnectionException($this->httpConfig->getUrl(), "Got Http response code {$httpStatus} when accessing {$this->httpConfig->getUrl()}.");
                $ex->setData($result);
                throw $ex;
            }
        }
        return $result;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @param array $handlers array of handlers
  * @param string $path   Resource path relative to base service endpoint
  * @param string $method HTTP method - one of GET, POST, PUT, DELETE, PATCH etc
  * @param string $data   Request payload
  * @param array $headers HTTP headers
  */
 public function execute($handlers, $path, $method, $data = '', $headers = array())
 {
     $config = $this->apiContext->getConfig();
     $httpConfig = new PPHttpConfig(null, $method);
     $httpConfig->setHeaders($headers + array('Content-Type' => 'application/json'));
     foreach ($handlers as $handler) {
         $handler = new $handler($this->apiContext);
         $handler->handle($httpConfig, $data, array('path' => $path));
     }
     $connection = new PPHttpConnection($httpConfig, $config);
     $response = $connection->execute($data);
     $this->logger->fine($response);
     return $response;
 }