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 $contentLength = null)
214 $curlSession = $this->prepareSession($resourcePath, $queryParameters, $mimeType, $contentType, $contentLength);
215 curl_setopt($curlSession, CURLOPT_POST,
true);
216 curl_setopt($curlSession, CURLOPT_POSTFIELDS, $payload);
217 return $this->performRequest($curlSession, $deserializationType);
237 public function delete($resourcePath, $queryParameters = array(),
238 $mimeType =
'application/vnd.maileon.api+xml',
239 $deserializationType = null)
241 $curlSession = $this->prepareSession($resourcePath, $queryParameters, $mimeType);
242 curl_setopt($curlSession, CURLOPT_CUSTOMREQUEST,
"DELETE");
243 return $this->performRequest($curlSession, $deserializationType);
246 private function prepareSession($resourcePath, $queryParameters, $mimeType, $contentType = null, $contentLength = null)
248 $requestUrl = $this->constructRequestUrl($resourcePath, $queryParameters);
249 $headers = $this->constructHeaders($mimeType, $contentType, $contentLength);
250 $curlSession = curl_init($requestUrl);
252 CURLOPT_HEADER =>
true,
253 CURLOPT_RETURNTRANSFER =>
true,
254 CURLOPT_HTTPHEADER => $headers,
255 CURLOPT_FAILONERROR =>
false,
256 CURLOPT_VERBOSE => $this->debug
260 $this->verboseOut = fopen(
"php://temp",
"rw+");
261 $options[CURLOPT_STDERR] = $this->verboseOut;
264 if ($this->timeout) {
269 if ($this->proxy_host) {
274 curl_setopt_array($curlSession, $options);
278 private function constructRequestUrl($resourcePath, $queryParameters)
280 $requestUrl = $this->configuration[
'BASE_URI'] .
"/" . $resourcePath;
282 if (isset($queryParameters) && !empty($queryParameters)) {
283 $requestUrl = $requestUrl .
'?';
285 foreach ($queryParameters as $key => $value) {
286 if (is_array($value)) {
287 foreach ($value as $innerKey => $innerValue) {
288 if ($innerValue ===
true) {
289 $requestUrl .= $innerValue .
'=true&';
290 }
else if ($value ===
false) {
291 $requestUrl .= $innerValue .
'=false&';
293 $requestUrl .= $key .
'=' . $innerValue .
'&';
297 if ($value ===
true) {
298 $requestUrl .= $key .
'=true&';
299 }
else if ($value ===
false) {
300 $requestUrl .= $key .
'=false&';
302 $requestUrl .= $key .
'=' . $value .
'&';
307 $requestUrl = rtrim($requestUrl,
'&');
313 private function constructHeaders($mimeType, $contentType = null, $contentLength = null)
316 "Content-type: " . ($contentType === null ? $mimeType : $contentType),
317 "Accept: " . $mimeType,
318 "Authorization: Basic " . $this->encodedApiKey,
322 if($contentLength !== null) { $headers []=
"Content-Length: " . $contentLength; }
338 private function performRequest($curlSession, $deserializationType = null)
340 $response = curl_exec($curlSession);
342 $response = $response ? $response : null;
345 $this->printDebugInformation($curlSession, $result);
346 curl_close($curlSession);
350 $this->printDebugInformation($curlSession, null, $this->throwException ? null : $e);
352 curl_close($curlSession);
353 if ($this->throwException) {
360 protected function appendArrayFields($params, $name, $fieldValues)
362 if (isset ($fieldValues) && is_array($fieldValues) && count($fieldValues) > 0) {
363 $params [
"$name"] = array();
364 foreach ($fieldValues as $value) {
365 $params [
"$name"] [] = urlencode($value);
371 private function printDebugInformation($curlSession, $result = null, $exception = null)
374 rewind($this->verboseOut);
375 $sessionLog = stream_get_contents($this->verboseOut);
376 $sessionLog = preg_replace(
"/^Authorization: .*$/m",
"Authorization: ***redacted***", $sessionLog);
377 if (defined(
'RUNNING_IN_PHPUNIT') && RUNNING_IN_PHPUNIT) {
379 echo
"cURL session log:\n";
380 echo $sessionLog .
"\n";
381 if ($result != null) {
383 echo $result->toString() .
"\n";
385 if ($exception != null) {
386 echo
"Caught exception:\n";
387 echo $exception .
"\n";
389 if (curl_errno($curlSession)) {
390 echo
"cURL Error: \n";
391 echo htmlentities(curl_error($curlSession));
394 echo
"<h3>cURL session log</h3>\n";
396 echo htmlentities($sessionLog);
398 if ($result != null) {
399 echo
"<h3>Result</h3>\n";
401 echo htmlentities($result->toString());
404 if ($exception != null) {
405 echo
"<h3>Exception</h3>\n";
407 echo htmlentities($exception);
410 if (curl_errno($curlSession)) {
411 echo
"<h3>cURL Error</h3>\n";
413 echo
'Curl error: ' . htmlentities(curl_error($curlSession));
417 $this->verboseOut = null;
__construct(array $config)
static $MAILEON_XML_MIME_TYPE
post($resourcePath, $payload="", $queryParameters=array(), $mimeType= 'application/vnd.maileon.api+xml', $deserializationType=null, $contentType=null, $contentLength=null)
put($resourcePath, $payload="", $queryParameters=array(), $mimeType= 'application/vnd.maileon.api+xml', $deserializationType=null)