70 private $verboseOut = null;
81 if (!array_key_exists(
'BASE_URI', $config) || !array_key_exists(
'API_KEY', $config)) {
82 apiError((get_class($this) .
': invalid config object (BASE_URI or API_KEY not set)'));
84 if (array_key_exists(
'THROW_EXCEPTION', $config)) {
85 $this->throwException = $config[
'THROW_EXCEPTION'];
87 if (array_key_exists(
'DEBUG', $config)) {
88 $this->debug = $config[
'DEBUG'];
92 if (array_key_exists(
'PROXY_HOST', $config)) {
93 $this->proxy_host = $config[
'PROXY_HOST'];
95 if (array_key_exists(
'PROXY_PORT', $config)) {
96 $this->proxy_port = $config[
'PROXY_PORT'];
100 if (array_key_exists(
'TIMEOUT', $config)) {
101 $this->timeout = $config[
'TIMEOUT'];
103 $this->configuration = $config;
104 $this->encodedApiKey = base64_encode($config[
'API_KEY']);
116 $this->debug = $isDebug;
146 public function get($resourcePath, $queryParameters = array(),
147 $mimeType =
'application/vnd.maileon.api+xml',
148 $deserializationType = null)
150 $curlSession = $this->prepareSession($resourcePath, $queryParameters, $mimeType);
151 return $this->performRequest($curlSession, $deserializationType);
173 public function put($resourcePath, $payload =
"", $queryParameters = array(),
174 $mimeType =
'application/vnd.maileon.api+xml',
175 $deserializationType = null)
177 $curlSession = $this->prepareSession($resourcePath, $queryParameters, $mimeType);
183 curl_setopt($curlSession, CURLOPT_CUSTOMREQUEST,
"PUT");
184 curl_setopt($curlSession, CURLOPT_POSTFIELDS, $payload);
185 return $this->performRequest($curlSession, $deserializationType);
208 public function post($resourcePath, $payload =
"", $queryParameters = array(),
209 $mimeType =
'application/vnd.maileon.api+xml',
210 $deserializationType = null)
212 $curlSession = $this->prepareSession($resourcePath, $queryParameters, $mimeType);
213 curl_setopt($curlSession, CURLOPT_POST,
true);
214 curl_setopt($curlSession, CURLOPT_POSTFIELDS, $payload);
215 return $this->performRequest($curlSession, $deserializationType);
235 public function delete($resourcePath, $queryParameters = array(),
236 $mimeType =
'application/vnd.maileon.api+xml',
237 $deserializationType = null)
239 $curlSession = $this->prepareSession($resourcePath, $queryParameters, $mimeType);
240 curl_setopt($curlSession, CURLOPT_CUSTOMREQUEST,
"DELETE");
241 return $this->performRequest($curlSession, $deserializationType);
244 private function prepareSession($resourcePath, $queryParameters, $mimeType)
246 $requestUrl = $this->constructRequestUrl($resourcePath, $queryParameters);
247 $headers = $this->constructHeaders($mimeType);
248 $curlSession = curl_init($requestUrl);
250 CURLOPT_HEADER =>
false,
251 CURLOPT_RETURNTRANSFER =>
true,
252 CURLOPT_HTTPHEADER => $headers,
253 CURLOPT_FAILONERROR =>
false,
254 CURLOPT_VERBOSE => $this->debug
258 $this->verboseOut = fopen(
"php://temp",
"rw+");
259 $options[CURLOPT_STDERR] = $this->verboseOut;
262 if ($this->timeout) {
267 if ($this->proxy_host) {
272 curl_setopt_array($curlSession, $options);
276 private function constructRequestUrl($resourcePath, $queryParameters)
278 $requestUrl = $this->configuration[
'BASE_URI'] .
"/" . $resourcePath;
280 if (isset($queryParameters) && !empty($queryParameters)) {
281 $requestUrl = $requestUrl .
'?';
283 foreach ($queryParameters as $key => $value) {
284 if (is_array($value)) {
285 foreach ($value as $innerKey => $innerValue) {
286 if ($innerValue ===
true) {
287 $requestUrl .= $innerValue .
'=true&';
288 }
else if ($value ===
false) {
289 $requestUrl .= $innerValue .
'=false&';
291 $requestUrl .= $key .
'=' . $innerValue .
'&';
295 if ($value ===
true) {
296 $requestUrl .= $key .
'=true&';
297 }
else if ($value ===
false) {
298 $requestUrl .= $key .
'=false&';
300 $requestUrl .= $key .
'=' . $value .
'&';
305 $requestUrl = rtrim($requestUrl,
'&');
311 private function constructHeaders($mimeType)
314 "Content-type: " . $mimeType,
315 "Accept: " . $mimeType,
316 "Authorization: Basic " . $this->encodedApiKey,
333 private function performRequest($curlSession, $deserializationType = null)
335 $response = curl_exec($curlSession);
337 $response = $response ? $response : null;
340 $this->printDebugInformation($curlSession, $result);
341 curl_close($curlSession);
345 $this->printDebugInformation($curlSession, null, $this->throwException ? null : $e);
347 curl_close($curlSession);
348 if ($this->throwException) {
355 protected function appendArrayFields($params, $name, $fieldValues)
357 if (isset ($fieldValues) && count($fieldValues) > 0) {
358 $params [
"$name"] = array();
359 foreach ($fieldValues as $value) {
360 $params [
"$name"] [] = urlencode($value);
366 private function printDebugInformation($curlSession, $result = null, $exception = null)
369 rewind($this->verboseOut);
370 $sessionLog = stream_get_contents($this->verboseOut);
371 $sessionLog = preg_replace(
"/^Authorization: .*$/m",
"Authorization: ***redacted***", $sessionLog);
372 if (defined(
'RUNNING_IN_PHPUNIT') && RUNNING_IN_PHPUNIT) {
374 echo
"cURL session log:\n";
375 echo $sessionLog .
"\n";
376 if ($result != null) {
378 echo $result->toString() .
"\n";
380 if ($exception != null) {
381 echo
"Caught exception:\n";
382 echo $exception .
"\n";
385 echo
"<h3>cURL session log</h3>\n";
387 echo htmlentities($sessionLog);
389 if ($result != null) {
390 echo
"<h3>Result</h3>\n";
392 echo htmlentities($result->toString());
395 if ($exception != null) {
396 echo
"<h3>Exception</h3>\n";
398 echo htmlentities($exception);
402 $this->verboseOut = null;
__construct(array $config)
post($resourcePath, $payload="", $queryParameters=array(), $mimeType= 'application/vnd.maileon.api+xml', $deserializationType=null)
static $MAILEON_XML_MIME_TYPE
put($resourcePath, $payload="", $queryParameters=array(), $mimeType= 'application/vnd.maileon.api+xml', $deserializationType=null)