/Kiss/Inxmail.php

https://github.com/Paratron/KISS-tools · PHP · 105 lines · 56 code · 17 blank · 32 comment · 2 complexity · dc32ba7b4a18367d90f2d5ece2949f43 MD5 · raw file

  1. <?php
  2. /**
  3. * Inxmail
  4. * =======
  5. *
  6. *
  7. * @author: Christian Engel <hello@wearekiss.com>
  8. * @version: 1 31.05.13
  9. */
  10. namespace kiss;
  11. class Inxmail {
  12. private $servlet;
  13. private $testDriveResult = NULL;
  14. private $listName;
  15. private $dumpFile = '';
  16. /**
  17. * Sets up a Hook to a new Inxmail Servlet.
  18. * @param {String} $servletName
  19. * @param string [$server=web.inxmail.com]
  20. */
  21. function __construct($servletName, $server = 'web.inxmail.com') {
  22. $this->servlet = 'http://' . $server . '/' . $servletName . '/subscription/servlet';
  23. }
  24. /**
  25. * @param $listName
  26. */
  27. function useList($listName) {
  28. $this->listName = $listName;
  29. }
  30. /**
  31. * Will dump all responses from Inxmail into the given file.
  32. * @param {String} $filename
  33. */
  34. function dumpResponse($filename){
  35. $this->dumpFile = $filename;
  36. }
  37. /**
  38. * Lets the send() method use a test-script instead of inxmail.
  39. * @param {String} $scriptURL The URL the data should be POSTed to.
  40. * @param {Bool} [$testDriveResult] Optional outcome definition of the test.
  41. */
  42. function testDrive($scriptURL, $testDriveResult = TRUE){
  43. $this->servlet = $scriptURL;
  44. $this->testDriveResult = $testDriveResult;
  45. }
  46. /**
  47. * After you selected a list, you can send data to Inxmail with this method.
  48. * Pass an array of values you want to send to Inxmail.
  49. * @param {array} $data Associative array with data to send to Inxmail.
  50. * @return {bool} Will return TRUE on a successful send; FALSE when Inxmail returned an error.
  51. */
  52. function send($data) {
  53. $c = curl_init($this->servlet);
  54. $data['INXMAIL_SUBSCRIPTION'] = $this->listName;
  55. $data['INXMAIL_HTTP_REDIRECT'] = 'http://example.com/success/';
  56. $data['INXMAIL_HTTP_REDIRECT_ERROR'] = 'http://example.com/error/';
  57. $data['INXMAIL_CHARSET'] = 'utf-8';
  58. curl_setopt($c, CURLOPT_FRESH_CONNECT, TRUE);
  59. curl_setopt($c, CURLOPT_FORBID_REUSE, 1);
  60. curl_setopt($c, CURLOPT_POST, TRUE);
  61. curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE);
  62. curl_setopt($c, CURLOPT_POSTFIELDS, http_build_query($data));
  63. curl_setopt($c, CURLOPT_FOLLOWLOCATION, FALSE);
  64. //curl_setopt($c, CURLOPT_PROXY, '127.0.0.1:8888');
  65. curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE);
  66. curl_setopt($c, CURLOPT_HEADER, TRUE);
  67. curl_setopt($c, CURLOPT_HTTPHEADER, array(
  68. 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  69. 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3',
  70. 'Accept-Encoding: gzip,deflate,sdch',
  71. 'Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4',
  72. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
  73. 'Connection: keep-alive',
  74. 'Origin: null',
  75. 'Cache-Control: max-age=0'
  76. ));
  77. $result = curl_exec($c);
  78. if($this->dumpFile){
  79. $f = fopen($this->dumpFile, 'a');
  80. fwrite($f, $result);
  81. fclose($f);
  82. }
  83. if($this->testDriveResult !== NULL){
  84. return $this->testDriveResult;
  85. }
  86. return stristr($result, 'http://example.com/error/') ? FALSE : TRUE;
  87. }
  88. }