-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCurlClient.php
More file actions
143 lines (143 loc) · 5.13 KB
/
Copy pathCurlClient.php
File metadata and controls
143 lines (143 loc) · 5.13 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
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
<?php
/*******************************************************************************
* Copyright (c) 2017 Intuit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
require ("./BaseCurl.php");
/**
* Class CurlHttpClient
*
* A Http Client using PHP cURL extension to send HTTP/HTTPS request to QuickBooks Online
*
*
*/
class CurlClient {
/**
* @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->prepareRequest($url, $method, $headers, $body, $timeOut, $verifySSL);
$rawResponse = $this->executeRequest();
$this->handleErrors();
$this->intuitResponse = $rawResponse;
$this->closeConnection();
return $this->getLastResponse();
}
/**
* @inheritdoc
*/
public function prepareRequest($url, $method, array $headers, $body, $timeOut, $verifySSL){
//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 => 10,
CURLOPT_TIMEOUT => isset($timeOut) ? $timeOut : 100,
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;
}
$this->intializeCurl();
$this->basecURL->setupCurlOptArray($curl_opt);
}
/**
* Send a request and return the response
* @return curlResponse
*/
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 \Exception("cURL error during making API call. cURL Error Number:[" . $errorNumber . "] with error:[" . $errorMsg . "]");
}
}
/**
* Check if the cURL instance exists. If not or closed, create a new BaseCurl instance for this Http client
*/
private function intializeCurl(){
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 \Exception("Error. The headers set for cURL are either NULL or Empty");
}else{
$convertedHeaders = $this->convertHeaderArrayToHeaders($headers);
return $convertedHeaders;
}
}
/**
* Convert an Array to Curl Headers
* @param array $headerArray The request headers
* @return Curl Array 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;
}
}