1
|
|
|
<?php |
2
|
|
|
namespace Lenius\Economic\API; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @class Economic_Request |
6
|
|
|
*/ |
7
|
|
|
class Request |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Contains Economic_Client instance |
11
|
|
|
* |
12
|
|
|
* @access protected |
13
|
|
|
*/ |
14
|
|
|
protected $client; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* __construct function. |
18
|
|
|
* |
19
|
|
|
* Instantiates the object |
20
|
|
|
* |
21
|
|
|
* @access public |
22
|
|
|
*/ |
23
|
4 |
|
public function __construct($client) |
24
|
|
|
{ |
25
|
4 |
|
$this->client = $client; |
26
|
4 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* GET function. |
30
|
|
|
* |
31
|
|
|
* Performs an API GET request |
32
|
|
|
* |
33
|
|
|
* @access public |
34
|
|
|
* @param string $path |
35
|
|
|
* @param array $query |
36
|
|
|
* @return Response |
37
|
|
|
*/ |
38
|
4 |
|
public function get($path, $query = array()) |
39
|
|
|
{ |
40
|
|
|
// Add query parameters to $path? |
41
|
4 |
|
if (!empty($query)) { |
42
|
|
|
if (strpos($path, '?') === false) { |
43
|
|
|
$path .= '?' . http_build_query($query, '', '&'); |
44
|
|
|
} else { |
45
|
|
|
$path .= ini_get('arg_separator.output') . http_build_query($query, '', '&'); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Set the request params |
50
|
4 |
|
$this->setUrl($path); |
51
|
|
|
|
52
|
|
|
// Start the request and return the response |
53
|
4 |
|
return $this->execute('GET'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* POST function. |
58
|
|
|
* |
59
|
|
|
* Performs an API POST request |
60
|
|
|
* |
61
|
|
|
* @access public |
62
|
|
|
* @return Response |
63
|
|
|
*/ |
64
|
|
|
public function post($path, $form = array()) |
65
|
|
|
{ |
66
|
|
|
// Set the request params |
67
|
|
|
$this->setUrl($path); |
68
|
|
|
|
69
|
|
|
// Start the request and return the response |
70
|
|
|
return $this->execute('POST', $form); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* PUT function. |
75
|
|
|
* |
76
|
|
|
* Performs an API PUT request |
77
|
|
|
* |
78
|
|
|
* @access public |
79
|
|
|
* @return Response |
80
|
|
|
*/ |
81
|
|
|
public function put($path, $form = array()) |
82
|
|
|
{ |
83
|
|
|
// Set the request params |
84
|
|
|
$this->setUrl($path); |
85
|
|
|
|
86
|
|
|
// Start the request and return the response |
87
|
|
|
return $this->execute('PUT', $form); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* PATCH function. |
92
|
|
|
* |
93
|
|
|
* Performs an API PATCH request |
94
|
|
|
* |
95
|
|
|
* @access public |
96
|
|
|
* @return Response |
97
|
|
|
*/ |
98
|
|
|
public function patch($path, $form = array()) |
99
|
|
|
{ |
100
|
|
|
// Set the request params |
101
|
|
|
$this->setUrl($path); |
102
|
|
|
|
103
|
|
|
// Start the request and return the response |
104
|
|
|
return $this->execute('PATCH', $form); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* DELETE function. |
109
|
|
|
* |
110
|
|
|
* Performs an API DELETE request |
111
|
|
|
* |
112
|
|
|
* @access public |
113
|
|
|
* @return Response |
114
|
|
|
*/ |
115
|
|
|
public function delete($path, $form = array()) |
116
|
|
|
{ |
117
|
|
|
// Set the request params |
118
|
|
|
$this->setUrl($path); |
119
|
|
|
|
120
|
|
|
// Start the request and return the response |
121
|
|
|
return $this->execute('DELETE', $form); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* setUrl function. |
126
|
|
|
* |
127
|
|
|
* Takes an API request string and appends it to the API url |
128
|
|
|
* |
129
|
|
|
* @access protected |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
4 |
|
protected function setUrl($params) |
133
|
|
|
{ |
134
|
4 |
|
curl_setopt($this->client->ch, CURLOPT_URL, Constants::API_URL . trim($params, '/')); |
135
|
4 |
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $request_type |
140
|
|
|
* @param array $form |
141
|
|
|
* @return Response |
142
|
|
|
* @throws Exception |
143
|
|
|
*/ |
144
|
4 |
|
protected function execute($request_type, $form = array()) |
145
|
|
|
{ |
146
|
|
|
// Set the HTTP request type |
147
|
4 |
|
curl_setopt($this->client->ch, CURLOPT_CUSTOMREQUEST, $request_type); |
148
|
|
|
|
149
|
|
|
// If additional data is delivered, we will send it along with the API request |
150
|
4 |
|
if (is_array($form) && ! empty($form)) { |
151
|
|
|
$post = json_encode($form); |
152
|
|
|
curl_setopt($this->client->ch, CURLOPT_POSTFIELDS, $post); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// Store received headers in temporary memory file, remember sent headers |
156
|
4 |
|
$fh_header = fopen('php://temp', 'w+'); |
157
|
4 |
|
curl_setopt($this->client->ch, CURLOPT_WRITEHEADER, $fh_header); |
158
|
4 |
|
curl_setopt($this->client->ch, CURLINFO_HEADER_OUT, true); |
159
|
|
|
|
160
|
|
|
// Execute the request |
161
|
4 |
|
$response_data = curl_exec($this->client->ch); |
162
|
|
|
|
163
|
4 |
|
if (curl_errno($this->client->ch) !== 0) { |
164
|
|
|
// An error occurred |
165
|
|
|
fclose($fh_header); |
166
|
|
|
throw new Exception(curl_error($this->client->ch), curl_errno($this->client->ch)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// Grab the headers |
170
|
4 |
|
$sent_headers = curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT); |
171
|
4 |
|
rewind($fh_header); |
172
|
4 |
|
$received_headers = stream_get_contents($fh_header); |
173
|
4 |
|
fclose($fh_header); |
174
|
|
|
|
175
|
|
|
// Retrieve the HTTP response code |
176
|
4 |
|
$response_code = (int) curl_getinfo($this->client->ch, CURLINFO_HTTP_CODE); |
177
|
|
|
|
178
|
|
|
// Return the response object. |
179
|
4 |
|
return new Response($response_code, $sent_headers, $received_headers, $response_data); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|