forked from klarna/php-xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurlTransport.php
More file actions
94 lines (85 loc) · 2.38 KB
/
CurlTransport.php
File metadata and controls
94 lines (85 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* File containing the CurlTransport class.
*
* PHP version 5.3
*
* @category Payment
* @package KlarnaAPI
* @author MINT <ms.modules@klarna.com>
* @copyright 2014 Klarna AB
* @license http://opensource.org/licenses/BSD-2-Clause BSD-2
* @link https://developers.klarna.com/
*/
/**
* CurlTransport
*
* @category Payment
* @package KlarnaAPI
* @author MINT <ms.modules@klarna.com>
* @copyright 2014 Klarna AB
* @license http://opensource.org/licenses/BSD-2-Clause BSD-2
* @link https://developers.klarna.com/
*/
class CurlTransport
{
/**
* @var CurlHandle
*/
protected $handle;
/**
* Constructor
*
* @param CurlHandle $handle Curl handle to use
* @param int $timeout Time-out in seconds
*/
public function __construct($handle, $timeout)
{
$this->handle = $handle;
$this->timeout = $timeout;
}
/**
* Get time-out seconds
*
* @return int Time-out seconds
*/
public function getTimeout()
{
return $this->timeout;
}
/**
* Send a request object
*
* @param object $request The request to send
*
* @throws KlarnaException For e.g. a timeout
*
* @return object A response to the request sent
*/
public function send($request)
{
$this->handle->init();
$this->handle->setOption(CURLOPT_URL, $request->getURL());
$this->handle->setOption(CURLOPT_HTTPHEADER, $request->getHeaders());
$this->handle->setOption(CURLOPT_RETURNTRANSFER, true);
$this->handle->setOption(CURLOPT_CONNECTTIMEOUT, $this->getTimeout());
$this->handle->setOption(CURLOPT_TIMEOUT, $this->getTimeout());
$this->handle->setOption(CURLOPT_SSL_VERIFYHOST, 2);
$this->handle->setOption(CURLOPT_SSL_VERIFYPEER, true);
$data = $this->handle->execute();
$info = $this->handle->getInfo();
$error = $this->handle->getError();
$this->handle->close();
/*
* A failure occurred if:
* payload is false (e.g. HTTP timeout?).
* info is false, then it has no HTTP status code.
*/
if (strlen($error) > 0) {
throw new KlarnaException(
"Connection failed with error: {$error}"
);
}
return $request->createResponse(intval($info['http_code']), $data);
}
}