-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
286 lines (222 loc) · 7.92 KB
/
Client.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* Adapted Mailjet Public API
* Adding namespaces and constants
*
* @package API v0.1
* @author Mailjet, Snowcap
* @link http://api.mailjet.com/
*
*/
namespace Snowcap\MailjetBundle;
class Client
{
const DEBUG_NONE = 0;
const DEBUG_ERRORS =1;
const DEBUG_ALL = 2;
/**
* @var string
*/
private $version = '0.1';
/**
* Choose your weapon : php, json, xml, serialize, html, csv
*
* @var string
*/
private $output = 'json';
/**
* Connect through https protocol
*
* @var bool
*/
private $secure = true;
/**
* Mode debug ? 0 none / 1 errors only / 2 all
*
* @var int
*/
private $debug = 2; //TODO: create constants
/**
* @var string
*/
private $apiKey;
/**
* @var string
*/
private $secretKey;
/**
* @param bool $apiKey
* @param bool $secretKey
*/
public function __construct($apiKey = false, $secretKey = false, $debug = self::DEBUG_NONE)
{
if ($apiKey) {
$this->apiKey = $apiKey;
}
if ($secretKey) {
$this->secretKey = $secretKey;
}
$this->apiUrl = (($this->secure) ? 'https' : 'http') . '://api.mailjet.com/' . $this->version . '';
$this->debug = $debug;
}
/**
* @param $method
* @param $args
* @return bool
*/
public function __call($method, $args)
{
# params
$params = (sizeof($args) > 0) ? $args[0] : array();
# request method
$request = isset($params["method"]) ? strtoupper($params["method"]) : 'GET';
# unset useless params
if (isset($params["method"])) {
unset($params["method"]);
}
# Make request
$result = $this->sendRequest($method, $params, $request);
# Return result
$return = ($result === true) ? $this->_response : false;
if ($this->debug == 2 || ($this->debug == 1 && $return == false)) {
$this->debug();
}
return $return;
}
/**
* @param $method
* @param array $params
* @param $request
* @return string
*/
public function requestUrlBuilder($method, $params = array(), $request)
{
$query_string = array('output' => 'output=' . $this->output);
foreach ($params as $key => $value) {
if ($request == "GET" || in_array($key, array('apikey', 'output'))) {
$query_string[$key] = $key . '=' . urlencode($value);
}
if ($key == "output") {
$this->output = $value;
}
}
$this->call_url = $this->apiUrl . '/' . $method . '/?' . join('&', $query_string);
return $this->call_url;
}
/**
* @param bool $method
* @param array $params
* @param string $request
* @return bool
*/
public function sendRequest($method = false, $params = array(), $request = "GET")
{
# Method
$this->_method = $method;
$this->_request = $request;
# Build request URL
$url = $this->requestUrlBuilder($method, $params, $request);
# Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl_handle, CURLOPT_USERPWD, $this->apiKey . ':' . $this->secretKey);
$this->_request_post = false;
if ($request == 'POST') :
curl_setopt($curl_handle, CURLOPT_POST, count($params));
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query($params, '', '&'));
$this->_request_post = $params;
endif;
$buffer = curl_exec($curl_handle);
if ($this->debug > 2) {
var_dump($buffer);
}
# Response code
$this->_response_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
# Close curl process
curl_close($curl_handle);
# RESPONSE
$this->_response = ($this->output == 'json') ? json_decode($buffer) : $buffer;
return ($this->_response_code == 200) ? true : false;
}
/**
*
*/
public function debug()
{
echo '<style type="text/css">';
echo '
#debugger {width: 100%; font-family: arial;}
#debugger table {padding: 0; margin: 0 0 20px; width: 100%; font-size: 11px; text-align: left;border-collapse: collapse;}
#debugger th, #debugger td {padding: 2px 4px;}
#debugger tr.h {background: #999; color: #fff;}
#debugger tr.Success {background:#90c306; color: #fff;}
#debugger tr.Error {background:#c30029 ; color: #fff;}
#debugger tr.Not-modified {background:orange ; color: #fff;}
#debugger th {width: 20%; vertical-align:top; padding-bottom: 8px;}
';
echo '</style>';
echo '<div id="debugger">';
if (isset($this->_response_code)) :
if ($this->_response_code == 200) :
echo '<table>';
echo '<tr class="Success"><th>Success</th><td></td></tr>';
echo '<tr><th>Status code</th><td>' . $this->_response_code . '</td></tr>';
if (isset($this->_response)) :
echo '<tr><th>Response</th><td><pre>' . utf8_decode(
print_r($this->_response, 1)
) . '</pre></td></tr>';
endif;
echo '</table>'; elseif ($this->_response_code == 304) :
echo '<table>';
echo '<tr class="Not-modified"><th>Error</th><td></td></tr>';
echo '<tr><th>Error no</th><td>' . $this->_response_code . '</td></tr>';
echo '<tr><th>Message</th><td>Not Modified</td></tr>';
echo '</table>'; else :
echo '<table>';
echo '<tr class="Error"><th>Error</th><td></td></tr>';
echo '<tr><th>Error no</th><td>' . $this->_response_code . '</td></tr>';
if (isset($this->_response)) :
if (is_array($this->_response) OR is_object($this->_response)):
echo '<tr><th>Status</th><td><pre>' . print_r(
$this->_response,
true
) . '</pre></td></tr>'; else:
echo '<tr><th>Status</th><td><pre>' . $this->_response . '</pre></td></tr>';
endif;
endif;
echo '</table>';
endif;
endif;
$call_url = parse_url($this->call_url);
echo '<table>';
echo '<tr class="h"><th>API config</th><td></td></tr>';
echo '<tr><th>Protocole</th><td>' . $call_url['scheme'] . '</td></tr>';
echo '<tr><th>Host</th><td>' . $call_url['host'] . '</td></tr>';
echo '<tr><th>Version</th><td>' . $this->version . '</td></tr>';
echo '</table>';
echo '<table>';
echo '<tr class="h"><th>Call infos</th><td></td></tr>';
echo '<tr><th>Method</th><td>' . $this->_method . '</td></tr>';
echo '<tr><th>Request type</th><td>' . $this->_request . '</td></tr>';
echo '<tr><th>Get Arguments</th><td>';
$args = explode("&", $call_url['query']);
foreach ($args as $arg) {
$arg = explode("=", $arg);
echo '' . $arg[0] . ' = <span style="color:#ff6e56;">' . $arg[1] . '</span><br/>';
}
echo '</td></tr>';
if ($this->_request_post) {
echo '<tr><th>Post Arguments</th><td>';
foreach ($this->_request_post as $k => $v) {
echo $k . ' = <span style="color:#ff6e56;">' . $v . '</span><br/>';
}
echo '</td></tr>';
}
echo '<tr><th>Call url</th><td>' . $this->call_url . '</td></tr>';
echo '</table>';
echo '</div>';
}
}