Overview
  • Namespace
  • Class

Namespaces

  • Klarna
    • XMLRPC
      • Exception

Interfaces

  • Klarna\XMLRPC\Exception\KlarnaException
 1 <?php
 2 /**
 3  * Copyright 2016 Klarna AB.
 4  *
 5  * Licensed under the Apache License, Version 2.0 (the "License");
 6  * you may not use this file except in compliance with the License.
 7  * You may obtain a copy of the License at
 8  *
 9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 namespace Klarna\XMLRPC;
18 
19 /**
20  * Class for handling curl requests.
21  */
22 class CurlTransport
23 {
24     /**
25      * Wrapper for the cURL resource.
26      *
27      * @var CurlHandle
28      */
29     protected $handle;
30 
31     /**
32      * Constructor.
33      *
34      * @param CurlHandle $handle  Curl handle to use
35      * @param int        $timeout Time-out in seconds
36      */
37     public function __construct($handle, $timeout)
38     {
39         $this->handle = $handle;
40         $this->timeout = $timeout;
41     }
42 
43     /**
44      * Get time-out seconds.
45      *
46      * @return int Time-out seconds
47      */
48     public function getTimeout()
49     {
50         return $this->timeout;
51     }
52 
53     /**
54      * Send a request object.
55      *
56      * @param object $request The request to send
57      *
58      * @throws Exceptions\KlarnaException For e.g. a timeout
59      *
60      * @return object A response to the request sent
61      */
62     public function send($request)
63     {
64         $this->handle->init();
65 
66         $this->handle->setOption(CURLOPT_URL, $request->getURL());
67         $this->handle->setOption(CURLOPT_HTTPHEADER, $request->getHeaders());
68         $this->handle->setOption(CURLOPT_RETURNTRANSFER, true);
69         $this->handle->setOption(CURLOPT_CONNECTTIMEOUT, $this->getTimeout());
70         $this->handle->setOption(CURLOPT_TIMEOUT, $this->getTimeout());
71         $this->handle->setOption(CURLOPT_SSL_VERIFYHOST, 2);
72         $this->handle->setOption(CURLOPT_SSL_VERIFYPEER, true);
73 
74         $data = $this->handle->execute();
75         $info = $this->handle->getInfo();
76         $error = $this->handle->getError();
77 
78         $this->handle->close();
79 
80         /*
81          * A failure occurred if:
82          * payload is false (e.g. HTTP timeout?).
83          * info is false, then it has no HTTP status code.
84          */
85         if (strlen($error) > 0) {
86             throw new Exceptions\KlarnaException(
87                 "Connection failed with error: {$error}"
88             );
89         }
90 
91         return $request->createResponse(intval($info['http_code']), $data);
92     }
93 }
94 
API documentation generated by ApiGen