Maileon PHP client  1.5.5
Easily integrate your PHP application with Maileon.
AbstractMaileonService.php
1 <?php
2 
12 {
13 
19  public static $MAILEON_XML_MIME_TYPE = 'application/vnd.maileon.api+xml';
20 
26  protected $configuration;
27 
33  protected $encodedApiKey;
34 
40  protected $debug = false;
41 
47  protected $throwException = true;
48 
54  protected $proxy_host;
55 
61  protected $proxy_port = 80;
62 
68  protected $timeout;
69 
70  private $verboseOut = null;
71 
78  public function __construct(array $config)
79  {
80  // check for valid configuration object
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)'));
83  }
84  if (array_key_exists('THROW_EXCEPTION', $config)) {
85  $this->throwException = $config['THROW_EXCEPTION'];
86  }
87  if (array_key_exists('DEBUG', $config)) {
88  $this->debug = $config['DEBUG'];
89  }
90 
91  // Proxy config
92  if (array_key_exists('PROXY_HOST', $config)) {
93  $this->proxy_host = $config['PROXY_HOST'];
94  }
95  if (array_key_exists('PROXY_PORT', $config)) {
96  $this->proxy_port = $config['PROXY_PORT'];
97  }
98 
99  // Timeout in seconds
100  if (array_key_exists('TIMEOUT', $config)) {
101  $this->timeout = $config['TIMEOUT'];
102  }
103  $this->configuration = $config;
104  $this->encodedApiKey = base64_encode($config['API_KEY']);
105  }
106 
114  public function setDebug($isDebug)
115  {
116  $this->debug = $isDebug;
117  }
118 
124  public function isDebug()
125  {
126  return $this->debug;
127  }
128 
146  public function get($resourcePath, $queryParameters = array(),
147  $mimeType = 'application/vnd.maileon.api+xml',
148  $deserializationType = null)
149  {
150  $curlSession = $this->prepareSession($resourcePath, $queryParameters, $mimeType);
151  return $this->performRequest($curlSession, $deserializationType);
152  }
153 
173  public function put($resourcePath, $payload = "", $queryParameters = array(),
174  $mimeType = 'application/vnd.maileon.api+xml',
175  $deserializationType = null)
176  {
177  $curlSession = $this->prepareSession($resourcePath, $queryParameters, $mimeType);
178  /*
179  * PUT does not work as expected when passing post data, see
180  * http://developers.sugarcrm.com/wordpress/2011/11/22/howto-do-put-requests-with-php-curl-without-writing-to-a-file/
181  * Because of this, we use a custom request here.
182  */
183  curl_setopt($curlSession, CURLOPT_CUSTOMREQUEST, "PUT");
184  curl_setopt($curlSession, CURLOPT_POSTFIELDS, $payload);
185  return $this->performRequest($curlSession, $deserializationType);
186  }
187 
208  public function post($resourcePath, $payload = "", $queryParameters = array(),
209  $mimeType = 'application/vnd.maileon.api+xml',
210  $deserializationType = null,
211  $contentType = null,
212  $contentLength = null)
213  {
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);
218  }
219 
237  public function delete($resourcePath, $queryParameters = array(),
238  $mimeType = 'application/vnd.maileon.api+xml',
239  $deserializationType = null)
240  {
241  $curlSession = $this->prepareSession($resourcePath, $queryParameters, $mimeType);
242  curl_setopt($curlSession, CURLOPT_CUSTOMREQUEST, "DELETE");
243  return $this->performRequest($curlSession, $deserializationType);
244  }
245 
246  private function prepareSession($resourcePath, $queryParameters, $mimeType, $contentType = null, $contentLength = null)
247  {
248  $requestUrl = $this->constructRequestUrl($resourcePath, $queryParameters);
249  $headers = $this->constructHeaders($mimeType, $contentType, $contentLength);
250  $curlSession = curl_init($requestUrl);
251  $options = array(
252  CURLOPT_HEADER => true,
253  CURLOPT_RETURNTRANSFER => true,
254  CURLOPT_HTTPHEADER => $headers,
255  CURLOPT_FAILONERROR => false,
256  CURLOPT_VERBOSE => $this->debug
257  );
258 
259  if ($this->debug) {
260  $this->verboseOut = fopen("php://temp", "rw+");
261  $options[CURLOPT_STDERR] = $this->verboseOut;
262  }
263 
264  if ($this->timeout) {
265  $options[CURLOPT_CONNECTTIMEOUT] = $this->timeout;
266  $options[CURLOPT_TIMEOUT] = $this->timeout;
267  }
268 
269  if ($this->proxy_host) {
270  $options[CURLOPT_PROXY] = $this->proxy_host;
271  $options[CURLOPT_PROXYPORT] = $this->proxy_port;
272  }
273 
274  curl_setopt_array($curlSession, $options);
275  return $curlSession;
276  }
277 
278  private function constructRequestUrl($resourcePath, $queryParameters)
279  {
280  $requestUrl = $this->configuration['BASE_URI'] . "/" . $resourcePath;
281 
282  if (isset($queryParameters) && !empty($queryParameters)) {
283  $requestUrl = $requestUrl . '?';
284 
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&';
292  } else {
293  $requestUrl .= $key . '=' . $innerValue . '&';
294  }
295  }
296  } else {
297  if ($value === true) {
298  $requestUrl .= $key . '=true&';
299  } else if ($value === false) {
300  $requestUrl .= $key . '=false&';
301  } else {
302  $requestUrl .= $key . '=' . $value . '&';
303  }
304  }
305  }
306 
307  $requestUrl = rtrim($requestUrl, '&');
308  }
309 
310  return $requestUrl;
311  }
312 
313  private function constructHeaders($mimeType, $contentType = null, $contentLength = null)
314  {
315  $headers = array(
316  "Content-type: " . ($contentType === null ? $mimeType : $contentType),
317  "Accept: " . $mimeType,
318  "Authorization: Basic " . $this->encodedApiKey,
319  "Expect:"
320  );
321 
322  if($contentLength !== null) { $headers []= "Content-Length: " . $contentLength; }
323 
324  return $headers;
325  }
326 
338  private function performRequest($curlSession, $deserializationType = null)
339  {
340  $response = curl_exec($curlSession);
341  // coerce all false values to null
342  $response = $response ? $response : null;
343  try {
344  $result = new com_maileon_api_MaileonAPIResult($response, $curlSession, $this->throwException, $deserializationType);
345  $this->printDebugInformation($curlSession, $result);
346  curl_close($curlSession);
347  return $result;
349  if ($this->debug) {
350  $this->printDebugInformation($curlSession, null, $this->throwException ? null : $e);
351  }
352  curl_close($curlSession);
353  if ($this->throwException) {
354  throw $e;
355  }
356  return null;
357  }
358  }
359 
360  protected function appendArrayFields($params, $name, $fieldValues)
361  {
362  if (isset ($fieldValues) && is_array($fieldValues) && count($fieldValues) > 0) {
363  $params ["$name"] = array();
364  foreach ($fieldValues as $value) {
365  $params ["$name"] [] = urlencode($value); //urlencode(utf8_encode($value));
366  }
367  }
368  return $params;
369  }
370 
371  private function printDebugInformation($curlSession, $result = null, $exception = null)
372  {
373  if ($this->debug) {
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) {
378  echo "\n";
379  echo "cURL session log:\n";
380  echo $sessionLog . "\n";
381  if ($result != null) {
382  echo "Result:\n";
383  echo $result->toString() . "\n";
384  }
385  if ($exception != null) {
386  echo "Caught exception:\n";
387  echo $exception . "\n";
388  }
389  if (curl_errno($curlSession)) {
390  echo "cURL Error: \n";
391  echo htmlentities(curl_error($curlSession));
392  }
393  } else {
394  echo "<h3>cURL session log</h3>\n";
395  echo "<pre>\n";
396  echo htmlentities($sessionLog);
397  echo "</pre>\n";
398  if ($result != null) {
399  echo "<h3>Result</h3>\n";
400  echo "<pre>\n";
401  echo htmlentities($result->toString());
402  echo "</pre>\n";
403  }
404  if ($exception != null) {
405  echo "<h3>Exception</h3>\n";
406  echo "<pre>\n";
407  echo htmlentities($exception);
408  echo "</pre>\n";
409  }
410  if (curl_errno($curlSession)) {
411  echo "<h3>cURL Error</h3>\n";
412  echo "<pre>\n";
413  echo 'Curl error: ' . htmlentities(curl_error($curlSession));
414  echo "</pre>\n";
415  }
416  }
417  $this->verboseOut = null;
418  }
419  }
420 }
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)