-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.omoney.http.helper.php
224 lines (206 loc) · 5.3 KB
/
class.omoney.http.helper.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
class OmoneyHttp {
public $_curl = null;
public $_headers = array();
/**
* Class constructor.
*
*/
public function __construct() {
$this->_initCurl();
}
/**
* Class destructor.
*
*/
public function __destruct() {
curl_close($this->_curl);
}
/**
* Initialize curl.
*
* Check if curl is enabled in the PHP environment.
* Trigger error if curl is not available.
* Initialize curl and set defaults.
*
* @return void
*/
private function _initCurl() {
if(!function_exists('curl_version')) {
trigger_error("Curl not available", E_USER_ERROR);
}
else {
$this->_curl = curl_init();
$this->_setDefaults();
}
}
/**
* Set curl defaults.
*
* @return void
*/
private function _setDefaults() {
curl_setopt($this->_curl, CURLOPT_VERBOSE, 1);
curl_setopt($this->_curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($this->_curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($this->_curl, CURLOPT_SSLVERSION , 'CURL_SSLVERSION_TLSv1_2');
curl_setopt($this->_curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->_curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($this->_curl, CURLOPT_TIMEOUT, 30);
curl_setopt($this->_curl, CURLOPT_HEADER, 1);
curl_setopt($this->_curl, CURLINFO_HEADER_OUT, 1);
}
/**
* Set curl headers using the class header array property.
*
* @return void
*/
private function _setHeaders() {
curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $this->_headers);
}
/**
* Execute curl request.
*
* Set headers for curl instance.
* Run curl instance.
* Return result or trigger warning.
*
* @return array Result of the curl request
*/
private function _sendRequest() {
$this->_setHeaders();
$result = curl_exec($this->_curl);
if(curl_errno($this->_curl)){
trigger_error("Request Error:" . curl_error($this->_curl), E_USER_WARNING);
}
$headerSize = curl_getinfo($this->_curl, CURLINFO_HEADER_SIZE);
$body = substr($result, $headerSize);
return json_decode($body, true);
}
/**
* Reset the helper.
*
* Re-initialize the curl instance.
* Reset class header array property.
*
* @return void
*/
public function resetHelper() {
$this->_curl = null;
$this->_initCurl();
$this->_headers = array();
}
/**
* Set curl url.
*
* @param string $url Url to be called using curl
* @return void
*/
public function setUrl($url) {
curl_setopt($this->_curl, CURLOPT_URL, $url);
}
/**
* Set body of the curl request.
*
* URL encode the request body.
* Check if data is array and url encode.
* Set the curl body.
* Set curl post options
*
* @param array|string $postData Curl request body
* @return void
*/
public function setBody($postData) {
if(is_array($postData)) {
$postData = json_encode($postData);
}
curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($this->_curl, CURLOPT_POST, true);
}
/**
* Set body of the curl request.
*
* URL encode the request body.
* Check if data is array and url encode.
* Set the curl body.
* Set curl method to PATCH
*
* @param array|string $data Curl request body
* @return void
*/
public function setPatchBody($data){
if(is_array($data)) {
$data = json_encode($data);
}
curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($this->_curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
}
/**
* Set body of the curl request.
*
* URL encode the request body.
* Check if data is array and url encode.
* Set the curl body.
* Set curl method to POST
*
* @param array|string $data Curl request body
* @return void
*/
public function setPostBody($data){
if(is_array($data)) {
$data = json_encode($data);
}
curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($this->_curl, CURLOPT_CUSTOMREQUEST, 'POST');
}
/**
* Set body of the curl request.
*
* URL encode the request body.
* Check if data is array and url encode.
* Set the curl body.
* Set curl method to GET
*
* @param array|string $data Curl request body
* @return void
*/
public function setGetBody($data){
if(is_array($data)) {
$data = json_encode($data);
}
curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($this->_curl, CURLOPT_CUSTOMREQUEST, 'GET');
}
/**
* Set authentication attributes of the curl request.
*
* @param string $authData Authentication credentials
* @return void
*/
public function setAuthentication($authData) {
curl_setopt($this->_curl, CURLOPT_USERPWD, $authData);
}
/**
* Push header values into class header array property.
*
* @param string $header Header key-value as string
* @return void
*/
public function addHeader($header) {
$this->_headers[] = $header;
}
/**
* Public function to start execution of curl request.
*
* Check if post type request and setup curl options.
* If post request, set curl body with required data.
* Call internal function to execute curl instance.
*
* @return array Curl response
*/
public function sendRequest() {
return $this->_sendRequest();
}
}
?>