Maileon PHP client  1.2.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  {
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);
216  }
217 
235  public function delete($resourcePath, $queryParameters = array(),
236  $mimeType = 'application/vnd.maileon.api+xml',
237  $deserializationType = null)
238  {
239  $curlSession = $this->prepareSession($resourcePath, $queryParameters, $mimeType);
240  curl_setopt($curlSession, CURLOPT_CUSTOMREQUEST, "DELETE");
241  return $this->performRequest($curlSession, $deserializationType);
242  }
243 
244  private function prepareSession($resourcePath, $queryParameters, $mimeType)
245  {
246  $requestUrl = $this->constructRequestUrl($resourcePath, $queryParameters);
247  $headers = $this->constructHeaders($mimeType);
248  $curlSession = curl_init($requestUrl);
249  $options = array(
250  CURLOPT_HEADER => false,
251  CURLOPT_RETURNTRANSFER => true,
252  CURLOPT_HTTPHEADER => $headers,
253  CURLOPT_FAILONERROR => false,
254  CURLOPT_VERBOSE => $this->debug
255  );
256 
257  if ($this->debug) {
258  $this->verboseOut = fopen("php://temp", "rw+");
259  $options[CURLOPT_STDERR] = $this->verboseOut;
260  }
261 
262  if ($this->timeout) {
263  $options[CURLOPT_CONNECTTIMEOUT] = $this->timeout;
264  $options[CURLOPT_TIMEOUT] = $this->timeout;
265  }
266 
267  if ($this->proxy_host) {
268  $options[CURLOPT_PROXY] = $this->proxy_host;
269  $options[CURLOPT_PROXYPORT] = $this->proxy_port;
270  }
271 
272  curl_setopt_array($curlSession, $options);
273  return $curlSession;
274  }
275 
276  private function constructRequestUrl($resourcePath, $queryParameters)
277  {
278  $requestUrl = $this->configuration['BASE_URI'] . "/" . $resourcePath;
279 
280  if (isset($queryParameters) && !empty($queryParameters)) {
281  $requestUrl = $requestUrl . '?';
282 
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&';
290  } else {
291  $requestUrl .= $key . '=' . $innerValue . '&';
292  }
293  }
294  } else {
295  if ($value === true) {
296  $requestUrl .= $key . '=true&';
297  } else if ($value === false) {
298  $requestUrl .= $key . '=false&';
299  } else {
300  $requestUrl .= $key . '=' . $value . '&';
301  }
302  }
303  }
304 
305  $requestUrl = rtrim($requestUrl, '&');
306  }
307 
308  return $requestUrl;
309  }
310 
311  private function constructHeaders($mimeType)
312  {
313  $headers = array(
314  "Content-type: " . $mimeType,
315  "Accept: " . $mimeType,
316  "Authorization: Basic " . $this->encodedApiKey,
317  "Expect:"
318  );
319  return $headers;
320  }
321 
333  private function performRequest($curlSession, $deserializationType = null)
334  {
335  $response = curl_exec($curlSession);
336  // coerce all false values to null
337  $response = $response ? $response : null;
338  try {
339  $result = new com_maileon_api_MaileonAPIResult($response, $curlSession, $this->throwException, $deserializationType);
340  $this->printDebugInformation($curlSession, $result);
341  curl_close($curlSession);
342  return $result;
344  if ($this->debug) {
345  $this->printDebugInformation($curlSession, null, $this->throwException ? null : $e);
346  }
347  curl_close($curlSession);
348  if ($this->throwException) {
349  throw $e;
350  }
351  return null;
352  }
353  }
354 
355  protected function appendArrayFields($params, $name, $fieldValues)
356  {
357  if (isset ($fieldValues) && count($fieldValues) > 0) {
358  $params ["$name"] = array();
359  foreach ($fieldValues as $value) {
360  $params ["$name"] [] = urlencode($value); //urlencode(utf8_encode($value));
361  }
362  }
363  return $params;
364  }
365 
366  private function printDebugInformation($curlSession, $result = null, $exception = null)
367  {
368  if ($this->debug) {
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) {
373  echo "\n";
374  echo "cURL session log:\n";
375  echo $sessionLog . "\n";
376  if ($result != null) {
377  echo "Result:\n";
378  echo $result->toString() . "\n";
379  }
380  if ($exception != null) {
381  echo "Caught exception:\n";
382  echo $exception . "\n";
383  }
384  } else {
385  echo "<h3>cURL session log</h3>\n";
386  echo "<pre>\n";
387  echo htmlentities($sessionLog);
388  echo "</pre>\n";
389  if ($result != null) {
390  echo "<h3>Result</h3>\n";
391  echo "<pre>\n";
392  echo htmlentities($result->toString());
393  echo "</pre>\n";
394  }
395  if ($exception != null) {
396  echo "<h3>Exception</h3>\n";
397  echo "<pre>\n";
398  echo htmlentities($exception);
399  echo "</pre>\n";
400  }
401  }
402  $this->verboseOut = null;
403  }
404  }
405 }
post($resourcePath, $payload="", $queryParameters=array(), $mimeType= 'application/vnd.maileon.api+xml', $deserializationType=null)
put($resourcePath, $payload="", $queryParameters=array(), $mimeType= 'application/vnd.maileon.api+xml', $deserializationType=null)