-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathCurlHttpClient.php
190 lines (168 loc) · 6.25 KB
/
CurlHttpClient.php
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace QuickBooksOnline\API\Core\HttpClients;
use QuickBooksOnline\API\Exception\SdkException;
use QuickBooksOnline\API\Core\CoreConstants;
/**
* Class CurlHttpClient
*
* A Http Client using PHP cURL extension to send HTTP/HTTPS request to QuickBooks Online
* @package QuickBooksOnline
*
*/
class CurlHttpClient implements HttpClientInterface{
/**
* @var BaseCurl The basecURL instance will be used for performing Http/Https client request.
*/
private $basecURL = null;
/**
* @var IntuitResponse | False The parsed response from curl client to Intuit customized response
*/
private $intuitResponse = false;
/**
* The constructor for constructing the cURL http client for making API calls
* @param BaseCurl $curl A predefined BaseCurl instance to be used in this client
*/
public function __construct(BaseCurl $curl = null)
{
if(isset($curl)){
$this->basecURL = $curl;
}else{
$this->basecURL = new BaseCurl();
}
}
/**
* @inheritdoc
*/
public function makeAPICall($url, $method, array $headers, $body, $timeOut, $verifySSL){
$this->clearResponse();
$this->prepareRequest($url, $method, $headers, $body, $timeOut, $verifySSL);
$rawResponse = $this->executeRequest();
$this->handleErrors();
$this->setIntuitResponse($rawResponse);
$this->closeConnection();
return $this->getLastResponse();
}
/**
* @inheritdoc
*/
public function prepareRequest($url, $method, array $headers, $body, $timeOut, $verifySSL){
if (defined('QUICKBOOKS_API_TIMEOUT')) {
// if the timeout constant is set, use it for the timeout
$timeOut = (int)QUICKBOOKS_API_TIMEOUT;
}
//Set basic Curl Info
$curl_opt = [
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => trim($method),
//Set return transfer to true so the curl_exec will return the result
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $this->getHeaders($headers),
//10 seconds is allowed to make the connection to the server
CURLOPT_CONNECTTIMEOUT => isset($timeOut) ? $timeOut : 15,
CURLOPT_TIMEOUT => isset($timeOut) ? $timeOut : 15,
CURLOPT_RETURNTRANSFER => true,
//When CURLOPT_HEADER is set to 0 the only effect is that header info from the response is excluded from the output.
//So if you don't need it that's a few less KBs that curl will return to you.
//In our case, header is required
CURLOPT_HEADER => true
];
if ($method !== "GET" && isset($body)) {
$curl_opt[CURLOPT_POSTFIELDS] = $body;
}
//Set SSL. Only Enabled for OAuth 2 Request
$this->setSSL($curl_opt, $verifySSL);
$this->initializeCurl();
$this->basecURL->setupCurlOptArray($curl_opt);
}
/**
* Before making any API call, clear the stored response from previous request.
*/
public function clearResponse(){
$this->intuitResponse = false;
}
/**
* Send a request and return the response
* @return mixed <b>TRUE</b> on success or <b>FALSE</b> on failure. However, if the <b>CURLOPT_RETURNTRANSFER</b>
*/
private function executeRequest(){
return $this->basecURL->execute();
}
/**
* The error during HTTP/HTTPS call. For example, the certificate expired. The server respond time out. It has nothing to do with the
* status code returned from server.
*/
private function handleErrors(){
if($this->basecURL->errno() || $this->basecURL->error()){
$errorMsg = $this->basecURL->error();
$errorNumber = $this->basecURL->errno();
throw new SdkException("cURL error during making API call. cURL Error Number:[" . $errorNumber . "] with error:[" . $errorMsg . "]");
}
}
/**
* @inheritdoc
*/
public function setIntuitResponse($response){
$headerSize = $this->basecURL->getInfo(CURLINFO_HEADER_SIZE);
$rawHeaders = mb_substr($response, 0, $headerSize);
$rawBody = mb_substr($response, $headerSize);
$httpStatusCode = $this->basecURL->getInfo(CURLINFO_HTTP_CODE);
$theIntuitResponse = new IntuitResponse($rawHeaders, $rawBody, $httpStatusCode, true);
$this->intuitResponse = $theIntuitResponse;
}
/**
* Check if the cURL instance exists. If not or closed, create a new BaseCurl instance for this Http client
*/
private function initializeCurl(){
if($this->basecURL->isCurlSet()){ return; }
else {$this->basecURL->init();}
}
/**
* Get headers for the QuciKbooks Online Response
*/
private function getHeaders($headers){
if(!isset($headers) || empty($headers)){
throw new SdkException("Error. The headers set for cURL are either NULL or Empty");
}else{
$convertedHeaders = $this->convertHeaderArrayToHeaders($headers);
return $convertedHeaders;
}
}
/**
* Set the SSL certifcate path and corresponding varaibles for cURL
*/
private function setSSL(&$curl_opt, $verifySSL){
$curl_opt[CURLOPT_SSL_VERIFYPEER] = true;
if($verifySSL){
$curl_opt[CURLOPT_SSL_VERIFYHOST] = 2;
//based on spec, if TLS 1.2 is supported, it will use the TLS 1.2 or latest version by default
//$curl_opt[CURLOPT_SSLVERSION] = 6;
$curl_opt[CURLOPT_CAINFO] = CoreConstants::getCertPath(); //Pem certification Key Path
} else {
$curl_opt[CURLOPT_SSL_VERIFYHOST] = 0;
}
}
/**
* Convert an Array to Curl Headers
* @param array $headerArray The request headers
* @return array Curl Headers
*/
public function convertHeaderArrayToHeaders(array $headerArray){
$headers = array();
foreach($headerArray as $k => $v){
$headers[] = $k . ":" . $v;
}
return $headers;
}
/**
* close the connection of current http client
*/
private function closeConnection(){
$this->basecURL->close();
}
/**
* @inheritdoc
*/
public function getLastResponse(){
return $this->intuitResponse;
}
}