Exemplo n.º 1
0
 /**
  * Test that factory method returns a instance that implements
  * the wanted interface
  *
  * @return void
  */
 public function testFactoryResultImplements()
 {
     $factory = new Klarna_Checkout_HTTP_CURLFactory;
     $result = $factory->handle();
     $this->assertInstanceOf(
         'Klarna_Checkout_HTTP_CURLHandleInterface',
         $result
     );
 }
Exemplo n.º 2
0
 /**
  * Performs a HTTP request.
  *
  * @param Klarna_Checkout_HTTP_Request $request the HTTP request to send.
  *
  * @throws RuntimeException                Thrown if a cURL handle cannot
  *                                         be initialized.
  * @throws Klarna_Checkout_ConnectionErrorException Thrown for unspecified
  *                                                  network or hardware issues.
  * @return Klarna_Checkout_HTTP_Response
  */
 public function send(Klarna_Checkout_HTTP_Request $request)
 {
     $curl = $this->curl->handle();
     if ($curl === false) {
         throw new RuntimeException('Failed to initialize a HTTP handle.');
     }
     $url = $request->getURL();
     $curl->setOption(CURLOPT_URL, $url);
     $method = $request->getMethod();
     if ($method === 'POST') {
         $curl->setOption(CURLOPT_POST, true);
         $curl->setOption(CURLOPT_POSTFIELDS, $request->getData());
     }
     // Convert headers to cURL format.
     $requestHeaders = array();
     foreach ($request->getHeaders() as $key => $value) {
         $requestHeaders[] = $key . ': ' . $value;
     }
     $curl->setOption(CURLOPT_HTTPHEADER, $requestHeaders);
     $curl->setOption(CURLOPT_RETURNTRANSFER, true);
     $curl->setOption(CURLOPT_CONNECTTIMEOUT, $this->timeout);
     $curl->setOption(CURLOPT_TIMEOUT, $this->timeout);
     $curlHeaders = new Klarna_Checkout_HTTP_CURLHeaders();
     $curl->setOption(CURLOPT_HEADERFUNCTION, array(&$curlHeaders, 'processHeader'));
     $curl->setOption(CURLOPT_SSL_VERIFYHOST, 2);
     $curl->setOption(CURLOPT_SSL_VERIFYPEER, true);
     // Override specific set options
     foreach ($this->options as $option => $value) {
         $curl->setOption($option, $value);
     }
     $payload = $curl->execute();
     $info = $curl->getInfo();
     $error = $curl->getError();
     $curl->close();
     /*
      * A failure occurred if:
      * payload is false (e.g. HTTP timeout?).
      * info is false, then it has no HTTP status code.
      */
     if ($payload === false || $info === false) {
         throw new Klarna_Checkout_ConnectionErrorException("Connection to '{$url}' failed: {$error}");
     }
     $headers = $curlHeaders->getHeaders();
     // Convert Content-Type into a normal header
     $headers['Content-Type'] = $info['content_type'];
     $response = new Klarna_Checkout_HTTP_Response($request, $headers, intval($info['http_code']), strval($payload));
     return $response;
 }