getresponse.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /**
  3. * GetResponse API v3 client library
  4. *
  5. * @author Pawel Maslak <pawel.maslak@getresponse.com>
  6. * @author Grzegorz Struczynski <grzegorz.struczynski@implix.com>
  7. *
  8. * @see http://apidocs.getresponse.com/en/v3/resources
  9. * @see https://github.com/GetResponse/getresponse-api-php
  10. */
  11. class GetResponse
  12. {
  13. private $api_key;
  14. private $api_url = 'https://api.getresponse.com/v3';
  15. private $timeout = 8;
  16. public $http_status;
  17. /**
  18. * X-Domain header value if empty header will be not provided
  19. * @var string|null
  20. */
  21. private $enterprise_domain = null;
  22. /**
  23. * X-APP-ID header value if empty header will be not provided
  24. * @var string|null
  25. */
  26. private $app_id = null;
  27. /**
  28. * Set api key and optionally API endpoint
  29. * @param $api_key
  30. * @param null $api_url
  31. */
  32. public function __construct($api_key, $api_url = null)
  33. {
  34. $this->api_key = $api_key;
  35. if (!empty($api_url)) {
  36. $this->api_url = $api_url;
  37. }
  38. }
  39. /**
  40. * We can modify internal settings
  41. * @param $key
  42. * @param $value
  43. */
  44. function __set($key, $value)
  45. {
  46. $this->{$key} = $value;
  47. }
  48. /**
  49. * get account details
  50. *
  51. * @return mixed
  52. */
  53. public function accounts()
  54. {
  55. return $this->call('accounts');
  56. }
  57. /**
  58. * @return mixed
  59. */
  60. public function ping()
  61. {
  62. return $this->accounts();
  63. }
  64. /**
  65. * Return all campaigns
  66. * @return mixed
  67. */
  68. public function getCampaigns()
  69. {
  70. return $this->call('campaigns');
  71. }
  72. /**
  73. * get single campaign
  74. * @param string $campaign_id retrieved using API
  75. * @return mixed
  76. */
  77. public function getCampaign($campaign_id)
  78. {
  79. return $this->call('campaigns/' . $campaign_id);
  80. }
  81. /**
  82. * adding campaign
  83. * @param $params
  84. * @return mixed
  85. */
  86. public function createCampaign($params)
  87. {
  88. return $this->call('campaigns', 'POST', $params);
  89. }
  90. /**
  91. * list all RSS newsletters
  92. * @return mixed
  93. */
  94. public function getRSSNewsletters()
  95. {
  96. $this->call('rss-newsletters', 'GET', null);
  97. }
  98. /**
  99. * send one newsletter
  100. *
  101. * @param $params
  102. * @return mixed
  103. */
  104. public function sendNewsletter($params)
  105. {
  106. return $this->call('newsletters', 'POST', $params);
  107. }
  108. /**
  109. * @param $params
  110. * @return mixed
  111. */
  112. public function sendDraftNewsletter($params)
  113. {
  114. return $this->call('newsletters/send-draft', 'POST', $params);
  115. }
  116. /**
  117. * add single contact into your campaign
  118. *
  119. * @param $params
  120. * @return mixed
  121. */
  122. public function addContact($params)
  123. {
  124. return $this->call('contacts', 'POST', $params);
  125. }
  126. /**
  127. * retrieving contact by id
  128. *
  129. * @param string $contact_id - contact id obtained by API
  130. * @return mixed
  131. */
  132. public function getContact($contact_id)
  133. {
  134. return $this->call('contacts/' . $contact_id);
  135. }
  136. /**
  137. * search contacts
  138. *
  139. * @param $params
  140. * @return mixed
  141. */
  142. public function searchContacts($params = null)
  143. {
  144. return $this->call('search-contacts?' . $this->setParams($params));
  145. }
  146. /**
  147. * retrieve segment
  148. *
  149. * @param $id
  150. * @return mixed
  151. */
  152. public function getContactsSearch($id)
  153. {
  154. return $this->call('search-contacts/' . $id);
  155. }
  156. /**
  157. * add contacts search
  158. *
  159. * @param $params
  160. * @return mixed
  161. */
  162. public function addContactsSearch($params)
  163. {
  164. return $this->call('search-contacts/', 'POST', $params);
  165. }
  166. /**
  167. * add contacts search
  168. *
  169. * @param $id
  170. * @return mixed
  171. */
  172. public function deleteContactsSearch($id)
  173. {
  174. return $this->call('search-contacts/' . $id, 'DELETE');
  175. }
  176. /**
  177. * get contact activities
  178. * @param $contact_id
  179. * @return mixed
  180. */
  181. public function getContactActivities($contact_id)
  182. {
  183. return $this->call('contacts/' . $contact_id . '/activities');
  184. }
  185. /**
  186. * retrieving contact by params
  187. * @param array $params
  188. *
  189. * @return mixed
  190. */
  191. public function getContacts($params = array())
  192. {
  193. return $this->call('contacts?' . $this->setParams($params));
  194. }
  195. /**
  196. * updating any fields of your subscriber (without email of course)
  197. * @param $contact_id
  198. * @param array $params
  199. *
  200. * @return mixed
  201. */
  202. public function updateContact($contact_id, $params = array())
  203. {
  204. return $this->call('contacts/' . $contact_id, 'POST', $params);
  205. }
  206. /**
  207. * drop single user by ID
  208. *
  209. * @param string $contact_id - obtained by API
  210. * @return mixed
  211. */
  212. public function deleteContact($contact_id)
  213. {
  214. return $this->call('contacts/' . $contact_id, 'DELETE');
  215. }
  216. /**
  217. * retrieve account custom fields
  218. * @param array $params
  219. *
  220. * @return mixed
  221. */
  222. public function getCustomFields($params = array())
  223. {
  224. return $this->call('custom-fields?' . $this->setParams($params));
  225. }
  226. /**
  227. * add custom field
  228. *
  229. * @param $params
  230. * @return mixed
  231. */
  232. public function setCustomField($params)
  233. {
  234. return $this->call('custom-fields', 'POST', $params);
  235. }
  236. /**
  237. * retrieve single custom field
  238. *
  239. * @param string $cs_id obtained by API
  240. * @return mixed
  241. */
  242. public function getCustomField($custom_id)
  243. {
  244. return $this->call('custom-fields/' . $custom_id, 'GET');
  245. }
  246. /**
  247. * retrieving billing information
  248. *
  249. * @return mixed
  250. */
  251. public function getBillingInfo()
  252. {
  253. return $this->call('accounts/billing');
  254. }
  255. /**
  256. * get single web form
  257. *
  258. * @param int $w_id
  259. * @return mixed
  260. */
  261. public function getWebForm($w_id)
  262. {
  263. return $this->call('webforms/' . $w_id);
  264. }
  265. /**
  266. * retrieve all webforms
  267. * @param array $params
  268. *
  269. * @return mixed
  270. */
  271. public function getWebForms($params = array())
  272. {
  273. return $this->call('webforms?' . $this->setParams($params));
  274. }
  275. /**
  276. * get single form
  277. *
  278. * @param int $form_id
  279. * @return mixed
  280. */
  281. public function getForm($form_id)
  282. {
  283. return $this->call('forms/' . $form_id);
  284. }
  285. /**
  286. * retrieve all forms
  287. * @param array $params
  288. *
  289. * @return mixed
  290. */
  291. public function getForms($params = array())
  292. {
  293. return $this->call('forms?' . $this->setParams($params));
  294. }
  295. /**
  296. * Curl run request
  297. *
  298. * @param null $api_method
  299. * @param string $http_method
  300. * @param array $params
  301. * @return mixed
  302. * @throws Exception
  303. */
  304. private function call($api_method = null, $http_method = 'GET', $params = array())
  305. {
  306. if (empty($api_method)) {
  307. return (object)array(
  308. 'httpStatus' => '400',
  309. 'code' => '1010',
  310. 'codeDescription' => 'Error in external resources',
  311. 'message' => 'Invalid api method'
  312. );
  313. }
  314. $params = json_encode($params);
  315. $url = $this->api_url . '/' . $api_method;
  316. $options = array(
  317. CURLOPT_URL => $url,
  318. CURLOPT_ENCODING => 'gzip,deflate',
  319. CURLOPT_FRESH_CONNECT => 1,
  320. CURLOPT_RETURNTRANSFER => 1,
  321. CURLOPT_TIMEOUT => $this->timeout,
  322. CURLOPT_HEADER => false,
  323. CURLOPT_USERAGENT => 'PHP GetResponse client 0.0.2',
  324. CURLOPT_HTTPHEADER => array('X-Auth-Token: api-key ' . $this->api_key, 'Content-Type: application/json'),
  325. );
  326. if (!empty($this->enterprise_domain)) {
  327. $options[CURLOPT_HTTPHEADER][] = 'X-Domain: ' . $this->enterprise_domain;
  328. }
  329. if (!empty($this->app_id)) {
  330. $options[CURLOPT_HTTPHEADER][] = 'X-APP-ID: ' . $this->app_id;
  331. }
  332. if ($http_method == 'POST') {
  333. $options[CURLOPT_POST] = 1;
  334. $options[CURLOPT_POSTFIELDS] = $params;
  335. } else if ($http_method == 'DELETE') {
  336. $options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
  337. }
  338. $curl = curl_init();
  339. curl_setopt_array($curl, $options);
  340. $response = json_decode(curl_exec($curl));
  341. $this->http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  342. curl_close($curl);
  343. return (object)$response;
  344. }
  345. /**
  346. * @param array $params
  347. *
  348. * @return string
  349. */
  350. private function setParams($params = array())
  351. {
  352. $result = array();
  353. if (is_array($params)) {
  354. foreach ($params as $key => $value) {
  355. $result[$key] = $value;
  356. }
  357. }
  358. return http_build_query($result);
  359. }
  360. }