Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp.php
More file actions
Latest commit
executable file
·254 lines (227 loc) · 6.5 KB
/
Copy pathhttp.php
File metadata and controls
executable file
·254 lines (227 loc) · 6.5 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
* LightPHP Framework
* LitePHP is a framework that has been designed to be lite waight, extensible and fast.
*
* @author Robert Pitt <robertpitt1988@gmail.com>
* @category core
* @copyright 2013 Robert Pitt
* @license GPL v3 - GNU Public License v3
* @version 1.0.0
*/
!defined('SECURE') && die('Access Forbidden!');
/**
* HTTP Request class
*/
class HTTP_Library
{
/**
* Useragent String
* @var string
*/
protected$_useragent = "LitePHP/HTTP Request Library";
/**
* Constructor
*/
publicfunction__construct()
{
/**
* Validate that curl is installed
*/
if(function_exists('curl_init') === false)
{
thrownewException("Curl is required for the HTTP library");
}
/**
* Make sure pecl_http is installed
*/
if(function_exists('http_build_str') === false)
{
thrownewException("pecl_http is required for the HTTP library");
}
}
/**
* Perform a GET Request
* @param String $url HTTP endpoint to connect to
* @param array $query Key/Value list of query parameters
* @param array $headers Key/Value list of headers
* @return Array Response Object
*/
publicfunctionget($url, $query = array(), $headers = array())
{
return$this->_request($url, 'GET', $query, array(), $headers);
}
/**
* Perform a POST Request
* @param String $url HTTP endpoint to connect to
* @param array $query Key/Value list of query parameters
* @param array $data Key/Value list of POST Values,
* you can also supply a file by using @/path/to/file
* @param array $headers Key/Value list of headers
* @return Array Response Object
*/
publicfunctionpost($url, $query = array(), $data = array(), $headers = array())
{
return$this->_request($url, 'POST', $query, $data, $headers);
}
/**
* Perform a PUT Request
* @param String $url HTTP endpoint to connect to
* @param array $query Key/Value list of query parameters
* @param array $data Key/Value list of POST Values,
* you can also supply a file by using @/path/to/file
* @param array $headers Key/Value list of headers
* @return Array Response Object
*/
publicfunctionput($url, $query = array(), $data = array(), $headers = array())
{
return$this->_request($url, 'PUT', $query, $data, $headers);
}
/**
* Perform a DELETE Request
* @param String $url HTTP endpoint to connect to
* @param array $query Key/Value list of query parameters
* @param array $headers Key/Value list of headers
* @return Array Response Object
*/
publicfunctiondelete($url, $query = array(), $headers = array())
{
return$this->_request($url, 'PUT', $query, array(), $headers);
}
/**
* Perform a OPTIONS Request
* @param String $url HTTP endpoint to connect to
* @param array $query Key/Value list of query parameters
* @param array $headers Key/Value list of headers
* @return Array Response Object
*/
publicfunctionoptions($url, $query = array(), $headers = array())
{
return$this->_request($url, 'OPTIONS', $query, array(), $headers);
}
/**
* Perform a TRACE Request
* @param String $url HTTP endpoint to connect to
* @param array $query Key/Value list of query parameters
* @param array $headers Key/Value list of headers
* @return Array Response Object
*/
publicfunctiontrace($url, $query = array(), $headers = array())
{
return$this->_request($url, 'TRACE', $query, array(), $headers);
}
/**
* Perform a CURL based request to an endpoint.
* @param string $url Target location
* @param string $method HTTP Method used for the connection
* @param array $query Key/Value pair of parameters for the url
* @param array $data Key/Value pair of parameters to be sent as the payload
* @param array $headers Key/Value pair of headers
* @return array Response array that contains headers and the body.
*/
publicfunction_request($url, $method = '', $query = array(), $data = array(), $headers = array())
{
/**
* Parse the url into its components
*/
$components = parse_url($url);
/**
* Merge the query entities if required.
*/
if(isset($components['query']))
{
/**
* Parse the query string into a key/value pair for merging
*/
parse_str($components['query'], $origQueryString);
/**
* Merge and regenerate teh query string for the url
*/
$query = http_build_str(array_merge($origQueryString, $query));
}
/**
* Set the query to the components
*/
$components['query'] = http_build_query($query);
/**
* Rebuild the new url
*/
$url = http_build_url($components);
/**
* Create a new curl object for this url
*/
$handle = curl_init();
/**
* Create the defualt curl options.
*/
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => $this->_useragent,
CURLOPT_HTTPHEADER => (array)$headers,
);
/**
* Switch what method we are attempting to perform this operation as.
*/
switch (strtoupper($method))
{
case'GET':
break;
case'POST':
$options[CURLOPT_POST] = true;
/**
* @todo we need to find an actual fix for this since
* we have to use a pseudo fix to pass in nested arrays through post
*/
if(isset($headers['Content-Type']) && $headers['Content-Type'] == "multipart/form-data"){
if(class_exists("CURLFile")){
foreach ($dataas$key => $value) {
if($value[0] == '@'){
$data[$key] = newCurlFile(substr($value, 1));
}
}
}
$options[CURLOPT_POSTFIELDS] = $data;
}else{
$options[CURLOPT_POSTFIELDS] = str_replace(array('%5B','%5D'), array('[',']'), http_build_query($data));
}
break;
case'PUT':
break;
case'DELETE':
break;
case'TRACE':
break;
}
/**
* Set the options to the handle
*/
curl_setopt_array($handle, $options);
/**
* Execute the response and return the error array if it has failed.
*/
if(!($response = curl_exec($handle)))
{
returnarray(
"error" => curl_errno($handle),
"no" => curl_errno($handle)
);
}
/**
* Get the information of the request
*/
$information = curl_getinfo($handle);
/**
* Close the curl handle
*/
curl_close($handle);
/**
* Return an array of the infomration and the response.
*/
returnarray(
"information" => $information,
"response" => $response
);
}
}