iContactProApi.php 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. <?php
  2. /**
  3. * @name iContactProApi
  4. * @package iContactPro
  5. * @author iContact <www.icontact.com>
  6. * @description This class is a wrapper for the iContactPro API.
  7. * It makes integrating iContactPro into your app as simple as
  8. * calling a method.
  9. * @version 2.0
  10. **/
  11. class iContactProApi {
  12. //////////////////////////////////////////////////////////////////////////////
  13. /// Properties //////////////////////////////////////////////////////////////
  14. ////////////////////////////////////////////////////////////////////////////
  15. protected static $oInstance = null; // This holds the instance of this class
  16. protected $iCompanyId = null; // This holds the company ID
  17. protected $iProfileId = null; // This holds the profile ID
  18. protected $aConfig = array(); // This is our array for pragmatically overriding configuration constants
  19. protected $aErrors = array(); // This holds the errors encountered with the iContactPro API
  20. protected $sLastRequest = null; // This holds the last request JSON
  21. protected $sLastResponse = null; // This holds the last response JSON
  22. protected $sRequestUri = null; // This stores the last used URL
  23. protected $aSearchParameters = array(); // This is our container for search params
  24. protected $iTotal = 0; // If the results return a total, it will be stored here
  25. protected $aWarnings = array(); // This holds the warnings encountered with the iContactPro API
  26. //////////////////////////////////////////////////////////////////////////////
  27. /// Singleton ///////////////////////////////////////////////////////////////
  28. ////////////////////////////////////////////////////////////////////////////
  29. /**
  30. * This sets the singleton pattern instance
  31. * @static
  32. * @access public
  33. * @param iContactProApi $oInstance Instance to set to
  34. * @return iContactProApi $this
  35. **/
  36. public static function setInstance($oInstance) {
  37. self::$oInstance = $oInstance;
  38. // Return instance of class
  39. return self::$oInstance;
  40. }
  41. /**
  42. * This gets the singleton instance
  43. * @static
  44. * @access public
  45. * @return iContactProApi $this
  46. **/
  47. public static function getInstance() {
  48. // Check to see if an instance has already
  49. // been created
  50. if (is_null(self::$oInstance)) {
  51. // If not, return a new instance
  52. self::$oInstance = new self();
  53. return self::$oInstance;
  54. } else {
  55. // If so, return the previously created
  56. // instance
  57. return self::$oInstance;
  58. }
  59. }
  60. /**
  61. * This resets the singleton instance to null
  62. * @static
  63. * @access public
  64. * @return void
  65. **/
  66. public static function resetInstance() {
  67. // Reset the instance
  68. self::$oInstance = null;
  69. }
  70. //////////////////////////////////////////////////////////////////////////////
  71. /// Constructor /////////////////////////////////////////////////////////////
  72. ////////////////////////////////////////////////////////////////////////////
  73. /**
  74. * This is our constuctor and simply checks for
  75. * defined constants and configuration values and
  76. * then builds the configuration from that
  77. * @access protected
  78. * @return iContactProApi $this
  79. **/
  80. protected function __construct() {
  81. // Check for constants
  82. $aConstantMap = array(
  83. // 'ICP_APIVERSION',
  84. 'ICP_APPID' => 'appId',
  85. // 'ICP_APIURL',
  86. 'ICP_APIUSERNAME' => 'apiUsername',
  87. 'ICP_APIPASSWORD' => 'apiPassword',
  88. 'ICP_COMPANYID' => 'companyId',
  89. 'ICP_PROFILEID' => 'profileId'
  90. );
  91. // Loop through the map
  92. foreach ($aConstantMap as $sConstant => $sConfigKey) {
  93. // Check for the defined constant
  94. if (defined($sConstant)) {
  95. // Set the configuration key to the contant's value
  96. $this->aConfig[$sConfigKey] = constant($sConstant);
  97. }
  98. }
  99. // Return instance
  100. return $this;
  101. }
  102. //////////////////////////////////////////////////////////////////////////////
  103. /// Public //////////////////////////////////////////////////////////////////
  104. ////////////////////////////////////////////////////////////////////////////
  105. /**
  106. * This method adds a contact to your iContactPro account
  107. * @access public
  108. * @param string $sEmail
  109. * @param string [$sStatus]
  110. * @param string [$sPrefix]
  111. * @param string [$sFirstName]
  112. * @param string [$sLastName]
  113. * @param string [$sSuffix]
  114. * @param string [$sStreet]
  115. * @param string [$sStreet2]
  116. * @param string [$sCity]
  117. * @param string [$sState]
  118. * @param string [$sPostalCode]
  119. * @param string [$sPhone]
  120. * @param string [$sFax]
  121. * @param string [$sBusiness]
  122. *
  123. * @return object
  124. **/
  125. public function addContact($sEmail, $sStatus = 'normal', $sPrefix = null, $sFirstName = null, $sLastName = null, $sSuffix = null, $sStreet = null, $sStreet2 = null, $sCity = null, $sState = null, $sPostalCode = null, $sPhone = null, $sFax = null, $sBusiness = null) {
  126. // Valid statuses
  127. $aValidStatuses = array('normal', 'bounced', 'donotcontact', 'pending', 'invitable', 'deleted');
  128. // Contact placeholder
  129. $aContact = array(
  130. 'email' => $sEmail
  131. );
  132. // Check for a prefix
  133. if (!empty($sPrefix)) {
  134. // Add the new prefix
  135. $aContact['prefix'] = (string) $sPrefix;
  136. }
  137. // Check for a first name
  138. if (!empty($sFirstName)) {
  139. // Add the new first name
  140. $aContact['firstName'] = (string) $sFirstName;
  141. }
  142. // Check for a last name
  143. if (!empty($sLastName)) {
  144. // Add the new last name
  145. $aContact['lastName'] = (string) $sLastName;
  146. }
  147. // Check for a suffix
  148. if (!empty($sSuffix)) {
  149. // Add the new suffix
  150. $aContact['suffix'] = (string) $sSuffix;
  151. }
  152. // Check for a street
  153. if (!empty($sStreet)) {
  154. // Add the new street
  155. $aContact['street'] = (string) $sStreet;
  156. }
  157. // Check for a street2
  158. if (!empty($sStreet2)) {
  159. // Add the new street 2
  160. $aContact['street2'] = (string) $sStreet2;
  161. }
  162. // Check for a city
  163. if (!empty($sCity)) {
  164. // Add the new city
  165. $aContact['city'] = (string) $sCity;
  166. }
  167. // Check for a state
  168. if (!empty($sState)) {
  169. // Add the new state
  170. $aContact['state'] = (string) $sState;
  171. }
  172. // Check for a postal code
  173. if (!empty($sPostalCode)) {
  174. // Add the new postal code
  175. $aContact['postalCode'] = (string) $sPostalCode;
  176. }
  177. // Check for a phone number
  178. if (!empty($sPhone)) {
  179. // Add the new phone number
  180. $aContact['phone'] = (string) $sPhone;
  181. }
  182. // Check for a fax number
  183. if (!empty($sFax)) {
  184. // Add the new fax number
  185. $aContact['fax'] = (string) $sFax;
  186. }
  187. // Check for a business name
  188. if (!empty($sBusiness)) {
  189. // Add the new business
  190. $aContact['business'] = (string) $sBusiness;
  191. }
  192. // Check for a valid status
  193. if (!empty($sStatus) && in_array($sStatus, $aValidStatuses)) {
  194. // Add the new status
  195. $aContact['status'] = $sStatus;
  196. } else {
  197. $aContact['status'] = 'normal';
  198. }
  199. // Make the call
  200. //echo("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/contacts");
  201. $aContacts = $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/contacts", 'POST', array($aContact), 'contacts');
  202. // Return the contact
  203. return $aContacts[0];
  204. }
  205. /**
  206. * This method adds a custom field or "term"
  207. * to the array of search parameters
  208. * @access public
  209. * @param string $sName
  210. * @param string $sValue
  211. * @return iContactProApi $this
  212. **/
  213. public function addCustomQueryField($sName, $sValue) {
  214. // Add the field
  215. $this->aSearchParameters[$sName] = (string) $sValue;
  216. // Return instance
  217. return $this;
  218. }
  219. /**
  220. * This message adds a list to your iContactPro account
  221. * @access public
  222. * @param string $sName
  223. * @param integer $iWelcomeMessageId
  224. * @param bool [$bEmailOwnerOnChange]
  225. * @param bool [$bWelcomeOnManualAdd]
  226. * @param bool [$bWelcomeOnSignupAdd]
  227. * @param string [$sDescription]
  228. * @param string [$sPublicName]
  229. * @return object
  230. **/
  231. public function addList($sName, $iWelcomeMessageId, $bEmailOwnerOnChange = true, $bWelcomeOnManualAdd = false, $bWelcomeOnSignupAdd = false, $sDescription = null, $sPublicName = null) {
  232. // Setup the list
  233. $aList = array(
  234. 'name' => $sName,
  235. 'welcomeMessageId' => $iWelcomeMessageId,
  236. 'emailOwnerOnChange' => intval($bEmailOwnerOnChange),
  237. 'welcomeOnManualAdd' => intval($bWelcomeOnManualAdd),
  238. 'welcomeOnSignupAdd' => intval($bWelcomeOnSignupAdd),
  239. 'description' => $sDescription,
  240. 'publicname' => $sPublicName
  241. );
  242. // Make the call
  243. $aLists = $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/lists", 'POST', array($aList), 'lists');
  244. // Return the list
  245. return $aLists[0];
  246. }
  247. /**
  248. * This method adds a message to
  249. * your iContactPro API account
  250. * @access public
  251. * @param string $sSubject
  252. * @param integer $iCampaignId
  253. * @param string [$sHtmlBody]
  254. * @param string [$sTextBody]
  255. * @param string [$sMessageName]
  256. * @param integer [$iListId]
  257. * @param string [$sMessageType]
  258. * @return object
  259. **/
  260. public function addMessage($sSubject, $iCampaignId, $sHtmlBody = null, $sTextBody = null, $sMessageName = null, $iListId = null, $sMessageType = 'normal') {
  261. // Valid message types
  262. $aValidMessageTypes = array('normal', 'autoresponder', 'welcome', 'confirmation');
  263. // Setup the message data
  264. $aMessage = array(
  265. 'campaignId' => $iCampaignId,
  266. 'htmlBody' => $sHtmlBody,
  267. 'messageName' => $sMessageName,
  268. 'messageType' => (in_array($sMessageType, $aValidMessageTypes) ? $sMessageType : 'normal'),
  269. 'subject' => $sSubject,
  270. 'textBody' => $sTextBody
  271. );
  272. // Add the message
  273. $aNewMessage = $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/messages", 'POST', array($aMessage), 'messages');
  274. // Return the message data
  275. return $aNewMessage[0];
  276. }
  277. /**
  278. * This method adds a field to the order by
  279. * key in the search parameters array
  280. * @access public
  281. * @param string $sField
  282. * @param string [$sDirection]
  283. * @return iContactProApi $this
  284. **/
  285. public function addOrderBy($sField, $sDirection = null) {
  286. // Check for existing order by parameters
  287. if (empty($this->aSearchParameters['orderby'])) {
  288. // Check for a direction
  289. if (empty($sDirection)) {
  290. // Add just the field
  291. $this->aSearchParameters['orderby'] = (string) $sField;
  292. } else {
  293. // Add the field and direction
  294. $this->aSearchParameters['orderby'] = (string) "{$sField}:{$sDirection}";
  295. }
  296. } else {
  297. // Check for a direction
  298. if (empty($sDirection)) {
  299. // Append just the field
  300. $this->aSearchParameters['orderby'] .= (string) ",{$sField}";
  301. } else {
  302. // Append the field and direction
  303. $this->aSearchParameters['orderby'] .= (string) ",{$sField}:{$sDirection}";
  304. }
  305. }
  306. // Return failure
  307. return false;
  308. }
  309. /**
  310. * This method handles the deleting of a single list
  311. * @access public
  312. * @param integer $iListId
  313. * @return bool
  314. **/
  315. public function deleteList($iListId) {
  316. // Delete the list
  317. return $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/lists/{$iListId}", 'delete');
  318. }
  319. /**
  320. * This method handles the handshaking between this app and the iContactPro API
  321. * @access public
  322. * @param string $sResource
  323. * @param string $sMethod
  324. * @param string $sReturnKey
  325. * @param mixed $mPostData Array, object, or string
  326. * @return array|object
  327. **/
  328. public function makeCall($sResource, $sMethod = 'GET', $mPostData = null, $sReturnKey = null) {
  329. // List of needed constants
  330. $aRequiredConfigs = array('apiPassword', 'apiUsername', 'appId', 'companyId', 'profileId');
  331. foreach ($aRequiredConfigs as $sKey) {
  332. if (empty($this->aConfig[$sKey])) {
  333. $this->addError("{$sKey} is undefined.");
  334. }
  335. }
  336. $sApiUrl = (string) "{$this->getUrl()}{$sResource}";
  337. $rHandle = curl_init();
  338. curl_setopt($rHandle, CURLOPT_HTTPHEADER, $this->getHeaders());
  339. curl_setopt($rHandle, CURLOPT_RETURNTRANSFER, true);
  340. if (defined('ICP_NOVERIFY') && constant('ICP_NOVERIFY')) {
  341. // conditionally disable SSL certificate verification
  342. curl_setopt($rHandle, CURLOPT_SSL_VERIFYPEER, false);
  343. }
  344. // Determine the request
  345. // method we are using
  346. switch (strtoupper($sMethod)) {
  347. // Deleting data
  348. case 'DELETE' :
  349. curl_setopt($rHandle, CURLOPT_CUSTOMREQUEST, 'DELETE');
  350. break;
  351. // Recieving data
  352. case 'GET' :
  353. if (!empty($this->aSearchParameters)) {
  354. $sApiUrl .= (string) '?'.http_build_query($this->aSearchParameters);
  355. }
  356. break;
  357. // Sending data
  358. case 'POST' :
  359. if (empty($mPostData)) {
  360. $this->addError('No POST data was provided.');
  361. } else {
  362. curl_setopt($rHandle, CURLOPT_POST, true);
  363. curl_setopt($rHandle, CURLOPT_POSTFIELDS, json_encode($mPostData));
  364. $this->sLastRequest = (string) json_encode($mPostData);
  365. }
  366. break;
  367. // Uploading data
  368. case 'PUT' :
  369. if (empty($mPostData)) {
  370. $this->addError('No file or data specified for PUT request');
  371. } elseif (!is_string($mPostData) || !file_exists($mPostData)) {
  372. // Not a file, so we assume this is just data
  373. curl_setopt($rHandle, CURLOPT_CUSTOMREQUEST, "PUT");
  374. curl_setopt($rHandle, CURLOPT_POSTFIELDS, $mPostData);
  375. } else {
  376. $rFileContentHandle = fopen($mPostData, 'r');
  377. if ($rFileContentHandle === false) {
  378. $this->addError('A non-existant file was specified for POST data, or the file could not be opened.');
  379. } else {
  380. // Found a file, so upload its contents
  381. curl_setopt($rHandle, CURLOPT_PUT, true);
  382. curl_setopt($rHandle, CURLOPT_INFILE, $rFileContentHandle);
  383. }
  384. }
  385. break;
  386. }
  387. // Store the URL into the instance
  388. $this->sRequestUri = (string) $sApiUrl;
  389. // Give our handle a URL
  390. curl_setopt($rHandle, CURLOPT_URL, $sApiUrl);
  391. // Try to execute the handle
  392. if (!$sResponse = curl_exec($rHandle)) {
  393. // Add an error, for we could
  394. // not even execute the handle
  395. $this->addError('We were unable to execute the cURL handle.');
  396. }
  397. // Set the response JSON
  398. $this->sLastResponse = (string) $sResponse;
  399. // Try to decode the response
  400. if ((!$aResponse = json_decode($sResponse)) && (strtoupper($sMethod) != 'DELETE')) {
  401. // Add an error, for the API
  402. // did not return valid JSON
  403. $this->addError('The iContactPro API did not return valid JSON.');
  404. }
  405. curl_close($rHandle);
  406. // Check for errors from the API
  407. if (!empty($aResponse->errors)) {
  408. // Loop through the errors
  409. foreach ($aResponse->errors as $sError) {
  410. // Add the error
  411. $this->addError($sError, 1);
  412. }
  413. }
  414. // Check for warnings from the API
  415. if (!empty($aResponse->warnings)) {
  416. // Loop through the warnings
  417. foreach ($aResponse->warnings as $sWarning) {
  418. // Add the warning
  419. $this->addWarning($sWarning);
  420. }
  421. }
  422. // Check for set errors
  423. if (!empty($this->aErrors)) {
  424. throw new Exception('Errors have occurred and the system cannot continue. Use getErrors() for details.');
  425. }
  426. // Check for a total
  427. if (!empty($aResponse->total)) {
  428. $this->iTotal = (integer) $aResponse->total;
  429. }
  430. // Return the response
  431. if (strtoupper($sMethod) == 'DELETE') {
  432. return true;
  433. } elseif (empty($sReturnKey)) {
  434. return $aResponse;
  435. } else {
  436. // Return the narrowed resposne
  437. return $aResponse->$sReturnKey;
  438. }
  439. }
  440. /**
  441. * This method sends a message
  442. * @access public
  443. * @param string $sIncludeListId
  444. * @param integer $iMessageId
  445. * @param string [$sExcludeListIds]
  446. * @param string [$sExcludeSegmentIds]
  447. * @param string [$sIncludeSegmentIds]
  448. * @param string [$sScheduledTime]
  449. * @return object
  450. **/
  451. public function sendMessage($sIncludeListIds, $iMessageId, $sExcludeListIds = null, $sExcludeSegmentIds = null, $sIncludeSegmentIds = null, $sScheduledTime = null) {
  452. // Send the message
  453. $aSends = $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/sends", 'POST', array(
  454. array(
  455. 'excludeListIds' => $sExcludeListIds,
  456. 'excludeSegmentIds' => $sExcludeSegmentIds,
  457. 'includeListIds' => $sIncludeListIds,
  458. 'includeSegmentIds' => $sIncludeSegmentIds,
  459. 'scheduledTime' => (empty($sScheduledTime) ? null : date('c', strtotime($sScheduledTime)))
  460. )
  461. ), 'sends');
  462. return $aSends;
  463. }
  464. /**
  465. * This method subscribes a contact to a list
  466. * @access public
  467. * @param integer $iContactId
  468. * @param integer $iListId
  469. * @param string $sStatus
  470. * @return object
  471. **/
  472. public function subscribeContactToList($iContactId, $iListId, $sStatus = 'normal') {
  473. // Valid statuses
  474. $aValidStatuses = array('normal', 'pending', 'unsubscribed');
  475. // Setup the subscription and make the call
  476. $aSubscriptions = $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/subscriptions", 'POST', array(
  477. array(
  478. 'contactId' => $iContactId,
  479. 'listId' => $iListId,
  480. 'status' => $sStatus
  481. )
  482. ), 'subscriptions');
  483. return $aSubscriptions;
  484. }
  485. /**
  486. * This method updates a contact in your iContactPro account
  487. * @access public
  488. * @param integer $iContactId
  489. * @param string $sEmail
  490. * @param string $sPrefix
  491. * @param string $sFirstName
  492. * @param string $sLastName
  493. * @param string $sSuffix
  494. * @param string $sStreet
  495. * @param string $sStreet2
  496. * @param string $sCity
  497. * @param string $sState
  498. * @param string $sPostalCode
  499. * @param string $sPhone
  500. * @param string $sFax
  501. * @param string $sBusiness
  502. * @param string $sStatus
  503. * @return bool|object
  504. **/
  505. public function updateContact($iContactId, $sEmail = null, $sPrefix = null, $sFirstName = null, $sLastName = null, $sSuffix = null, $sStreet = null, $sStreet2 = null, $sCity = null, $sState = null, $sPostalCode = null, $sPhone = null, $sFax = null, $sBusiness = null, $sStatus = null) {
  506. // Valid statuses
  507. $aValidStatuses = array('normal', 'bounced', 'donotcontact', 'pending', 'invitable', 'deleted');
  508. // Contact placeholder
  509. $aContact = array();
  510. // Check for an email address
  511. if (!empty($sEmail)) {
  512. // Add the new email
  513. $aContact['email'] = (string) $sEmail;
  514. }
  515. // Check for a prefix
  516. if (!empty($sPrefix)) {
  517. // Add the new prefix
  518. $aContact['prefix'] = (string) $sPrefix;
  519. }
  520. // Check for a first name
  521. if (!empty($sFirstName)) {
  522. // Add the new first name
  523. $aContact['firstName'] = (string) $sFirstName;
  524. }
  525. // Check for a last name
  526. if (!empty($sLastName)) {
  527. // Add the new last name
  528. $aContact['lastName'] = (string) $sLastName;
  529. }
  530. // Check for a suffix
  531. if (!empty($sSuffix)) {
  532. // Add the new suffix
  533. $aContact['suffix'] = (string) $sSuffix;
  534. }
  535. // Check for a street
  536. if (!empty($sStreet)) {
  537. // Add the new street
  538. $aContact['street'] = (string) $sStreet;
  539. }
  540. // Check for a street2
  541. if (!empty($sStreet2)) {
  542. // Add the new street 2
  543. $aContact['street2'] = (string) $sStreet2;
  544. }
  545. // Check for a city
  546. if (!empty($sCity)) {
  547. // Add the new city
  548. $aContact['city'] = (string) $sCity;
  549. }
  550. // Check for a state
  551. if (!empty($sState)) {
  552. // Add the new state
  553. $aContact['state'] = (string) $sState;
  554. }
  555. // Check for a postal code
  556. if (!empty($sPostalCode)) {
  557. // Add the new postal code
  558. $aContact['postalCode'] = (string) $sPostalCode;
  559. }
  560. // Check for a phone number
  561. if (!empty($sPhone)) {
  562. // Add the new phone number
  563. $aContact['phone'] = (string) $sPhone;
  564. }
  565. // Check for a fax number
  566. if (!empty($sFax)) {
  567. // Add the new fax number
  568. $aContact['fax'] = (string) $sFax;
  569. }
  570. // Check for a business name
  571. if (!empty($sBusiness)) {
  572. // Add the new business
  573. $aContact['business'] = (string) $sBusiness;
  574. }
  575. // Check for a valid status
  576. if (!empty($sStatus) && in_array($sStatus, $aValidStatuses)) {
  577. // Add the new status
  578. $aContact['status'] = $sStatus;
  579. }
  580. // Make sure the contact isn't empty
  581. if (!empty($aContact)) {
  582. // Make the call
  583. $oContact = $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/contacts/{$iContactId}", 'POST', array($aContact), 'contact');
  584. // Return the contact
  585. return $oContact;
  586. }
  587. // Inevitably return failure
  588. return false;
  589. }
  590. /**
  591. * This method uploads a CSV file to the iContactPro API
  592. * @access public
  593. * @param string $sFile
  594. * @param integer [$iListId]
  595. * @param integer [$iUploadId]
  596. * @return string|bool
  597. **/
  598. public function uploadData($sFile, $iListId = null, $iUploadId = null) {
  599. // Check for an upload ID
  600. if (empty($iUploadId)) {
  601. // Make the call
  602. $aUploads = $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/uploads", 'POST', array(
  603. array(
  604. 'action' => 'add',
  605. 'listIds' => $iListId
  606. )
  607. ), 'uploads');
  608. // Store the uploadID
  609. $iUploadId = $aUploads[0]->uploadId;
  610. }
  611. // Upload the data
  612. if ($this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/uploads/{$iUploadId}/data", 'PUT', $sFile, 'uploadId')) {
  613. // Loop until the upload is complete
  614. while (true) {
  615. // Grab the upload
  616. $aUpload = $this->getUpload($iUploadId);
  617. // Check to see if the upload
  618. // has finished uploading
  619. if ($aUpload->status != 'receiving') {
  620. // Return the upload
  621. return $this->makeCall("/a/{$this->getCompanyId()}/c{$this->getProfileId()}/uploads/{$iUploadId}/data", 'GET');
  622. }
  623. }
  624. }
  625. // Return failure
  626. return false;
  627. }
  628. /**
  629. * This message updates a list on your iContactPro account
  630. * @access public
  631. * @param string $sName
  632. * @param integer $iListId
  633. * @param string $sName
  634. * @param integer $iWelcomeMessageId
  635. * @param bool [$bEmailOwnerOnChange]
  636. * @param bool [$bWelcomeOnManualAdd]
  637. * @param bool [$bWelcomeOnSignupAdd]
  638. * @param string [$sDescription]
  639. * @param string [$sPublicName]
  640. * @return object
  641. **/
  642. public function updateList($iListId, $sName, $iWelcomeMessageId, $bEmailOwnerOnChange = true, $bWelcomeOnManualAdd = false, $bWelcomeOnSignupAdd = false, $sDescription = null, $sPublicName = null) {
  643. // Setup the list
  644. $aList = array(
  645. 'name' => $sName,
  646. 'welcomeMessageId' => $iWelcomeMessageId,
  647. 'emailOwnerOnChange' => intval($bEmailOwnerOnChange),
  648. 'welcomeOnManualAdd' => intval($bWelcomeOnManualAdd),
  649. 'welcomeOnSignupAdd' => intval($bWelcomeOnSignupAdd),
  650. 'description' => $sDescription,
  651. 'publicname' => $sPublicName
  652. );
  653. // Return the list
  654. return $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/lists/{$iListId}", 'POST', $aList, 'list');;
  655. }
  656. //////////////////////////////////////////////////////////////////////////////
  657. /// PROTECTED ///////////////////////////////////////////////////////////////
  658. ////////////////////////////////////////////////////////////////////////////
  659. /**
  660. * This method appends an error to the list
  661. * of errors encountered with the iContactPro API
  662. * @access protected
  663. * @param string $sText
  664. * @param integer [$iCode]
  665. * @return iContactProApi $this
  666. **/
  667. protected function addError($sText) {
  668. // Append the error
  669. array_push($this->aErrors, $sText);
  670. // Return instance
  671. return $this;
  672. }
  673. /**
  674. * This method appends a warning to the list
  675. * of warnings encountered with the iContactPro API
  676. * @access protected
  677. * @param string $sText
  678. * @return iContactProApi $this
  679. **/
  680. public function addWarning($sText) {
  681. // Append the warning
  682. array_push($this->aWarnings, $sText);
  683. // Return instance
  684. return $this;
  685. }
  686. //////////////////////////////////////////////////////////////////////////////
  687. /// Getters /////////////////////////////////////////////////////////////////
  688. ////////////////////////////////////////////////////////////////////////////
  689. /**
  690. * This method grabs the campaigns associated
  691. * your iContactPro account
  692. * @access public
  693. * @return object
  694. **/
  695. public function getCampaigns() {
  696. // Make the call and return the data
  697. return $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/campaigns", 'GET');
  698. }
  699. /**
  700. * This method returns the companyId. If the ID has
  701. * not been set, it sets it from the configs.
  702. *
  703. * @access public
  704. * @return integer
  705. */
  706. public function getCompanyId() {
  707. if (empty($this->iCompanyId)) {
  708. $this->setCompanyId();
  709. }
  710. return $this->iCompanyId;
  711. }
  712. /**
  713. * This method grabs a single contact
  714. * from your iContactPro account
  715. * @access public
  716. * @param integer $iContactId
  717. * @return object
  718. **/
  719. public function getContact($iContactId) {
  720. // Make the call and return the data
  721. return $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/contacts/{$iContactId}", 'GET', null, 'contact');
  722. }
  723. /**
  724. * This method grabs the contacts associated
  725. * with you iContactPro API account
  726. * @access public
  727. * @return array
  728. **/
  729. public function getContacts() {
  730. // Make the call and return the data
  731. return $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/contacts", 'GET');
  732. }
  733. /**
  734. * This method returns any set
  735. * errors in the current instance
  736. * @access public
  737. * @return array|bool
  738. **/
  739. public function getErrors() {
  740. // Check for errors
  741. if (empty($this->aErrors)) {
  742. return false;
  743. } else {
  744. return $this->aErrors;
  745. }
  746. }
  747. /**
  748. * This method builds the header array
  749. * for making calls to the API
  750. * @access public
  751. * @return array
  752. **/
  753. public function getHeaders() {
  754. // Return the headers
  755. return array(
  756. 'Except:',
  757. 'Accept: application/json',
  758. 'Content-type: application/json',
  759. 'Api-Version: ' . (defined('ICP_APIVERSION') ? constant('ICP_APIVERSION') : '2.2'),
  760. 'Api-AppId: ' . (!empty($this->aConfig['appId']) ? $this->aConfig['appId'] : (defined('ICP_APPID') ? constant('ICP_APPID') : '')),
  761. 'Api-Username: '. (!empty($this->aConfig['apiUsername']) ? $this->aConfig['apiUsername'] : (defined('ICP_APIUSERNAME') ? constant('ICP_APIUSERNAME') : '')),
  762. 'Api-Password: '. (!empty($this->aConfig['apiPassword']) ? $this->aConfig['apiPassword'] : (defined('ICP_APIPASSWORD') ? constant('ICP_APIPASSWORD') : ''))
  763. );
  764. }
  765. /**
  766. * This method returns the last
  767. * API POST request JSON
  768. * @access public
  769. * @param bool [$bDecode]
  770. * @return string|object
  771. **/
  772. public function getLastRequest($bDecode = false) {
  773. if ($bDecode === true) {
  774. return json_decode($this->sLastRequest);
  775. }
  776. return $this->sLastRequest;
  777. }
  778. /**
  779. * This method returns the last
  780. * API response JSON
  781. * @access public
  782. * @param bool [$bDecode]
  783. * @return string|object
  784. **/
  785. public function getLastResponse($bDecode = false) {
  786. if ($bDecode === true) {
  787. return json_decode($this->sLastResponse);
  788. }
  789. return $this->sLastResponse;
  790. }
  791. /**
  792. * This method grabs a list of lists
  793. * that are associated with you iContactPro
  794. * API account
  795. * @access public
  796. * @return array
  797. **/
  798. public function getLists() {
  799. // Make the call and return the lists
  800. return $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/lists", 'GET', null, 'lists');
  801. }
  802. /**
  803. * This method lists the opens of a
  804. * single message based on the messageID
  805. * @access public
  806. * @param integer iMessageId
  807. * @return integer
  808. **/
  809. public function getMessageOpens($iMessageId) {
  810. // Make the call and return the data
  811. return $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/messages/{$iMessageId}/opens", 'GET', null, 'total');
  812. }
  813. public function getMessages($sType = null) {
  814. // Check for a message type
  815. if (!empty($sType)) {
  816. $this->addCustomQueryField('messageType', $sType);
  817. }
  818. // Return the messages
  819. return $this->makeCall("/a/{$this->getCompanyId()}/c/{$this->getProfileId()}/messages", 'GET', null, 'messages');
  820. }
  821. /**
  822. * This method returns the profileId. If the ID has
  823. * not been set, it sets it from the configs.
  824. *
  825. * @access public
  826. * @return integer
  827. */
  828. public function getProfileId() {
  829. if (empty($this->iProfileId)) {
  830. $this->setProfileId();
  831. }
  832. return $this->iProfileId;
  833. }
  834. /**
  835. * This method returns the URL
  836. * that the last API request
  837. * called
  838. * @access public
  839. * @return string
  840. **/
  841. public function getRequestUri() {
  842. // Return the URL
  843. return $this->sRequestUri;
  844. }
  845. /**
  846. * This method returns the count of the
  847. * total number of records from the most
  848. * recent API call, if there is one
  849. * @access public
  850. * @return integer
  851. **/
  852. public function getTotal() {
  853. // Return the total records
  854. return $this->iTotal;
  855. }
  856. /**
  857. * This method simply returns the base URL for
  858. * your API
  859. * @access public
  860. * @param bool [$bFull]
  861. * @return string
  862. **/
  863. public function getUrl($bFull = false) {
  864. // Set the production URL
  865. $sBaseUrl = defined('ICP_APIURL') ? constant('ICP_APIURL') : 'https://api.icpro.co/icp';
  866. // Do we need to return the entire url or just
  867. // the base url of the API service
  868. if ($bFull === false) {
  869. // Return the base url
  870. return $sBaseUrl;
  871. } else {
  872. // Return the base url and account details
  873. return $sBaseUrl . "/a/{$this->getCompanyId()}/c/{$this->getProfileId()}";
  874. }
  875. }
  876. /**
  877. * This method grabs a specific upload
  878. * @access public
  879. * @param integer $iUploadId
  880. * @return object
  881. **/
  882. public function getUpload($iUploadId) {
  883. // Return the upload data
  884. return $this->makeCall("/a/{$this->getCompanyId()}/c{$this->getProfileId()}/uploads/{$iUploadId}/data");
  885. }
  886. /**
  887. * This method grabs the uploads associated
  888. * with your iContactPro account
  889. * @access public
  890. * @return array
  891. **/
  892. public function getUploads() {
  893. // Return the uploads
  894. return $this->makeCall("/a/{$this->getCompanyId()}/c{$this->getProfileId()}/uploads");
  895. }
  896. /**
  897. * This method returns the warnings encountered
  898. * while communicating with the iContactPro API
  899. * @access public
  900. * @return array
  901. **/
  902. public function getWarnings() {
  903. // Return the current system warnings
  904. return $this->aWarnings;
  905. }
  906. //////////////////////////////////////////////////////////////////////////////
  907. /// Setters /////////////////////////////////////////////////////////////////
  908. ////////////////////////////////////////////////////////////////////////////
  909. /**
  910. * This method allows the companyId specified in the configs
  911. * to be overridden
  912. *
  913. * @access public
  914. * @param integer [$iCompanyId]
  915. */
  916. public function setCompanyId($iCompanyId = null) {
  917. if (!empty ($iCompanyId)) {
  918. $this->iCompanyId = (integer) $iCompanyId;
  919. } else {
  920. $this->iCompanyId = (integer) (!empty($this->aConfig['companyId']) ? $this->aConfig['companyId'] : constant('ICP_COMPANYID'));
  921. }
  922. }
  923. /**
  924. * This method sets configuration into the
  925. * plugin to pragmatically override constants
  926. * @access public
  927. * @param array $aConfig
  928. * @return iContactProApi $this
  929. **/
  930. public function setConfig($aConfig) {
  931. // Combine the arrays
  932. $this->aConfig = (array) array_merge($this->aConfig, $aConfig);
  933. // Return instance
  934. return $this;
  935. }
  936. /**
  937. * This method sets the result limit
  938. * for GET requests to the iContactPro API
  939. * @access public
  940. * @param integer $iLimit
  941. * @return iContactProApi $this
  942. **/
  943. public function setLimit($iLimit) {
  944. // Set the limit in the search parameters
  945. $this->aSearchParameters['limit'] = (integer) $iLimit;
  946. // Return instance
  947. return $this;
  948. }
  949. /**
  950. * This method sets the result index
  951. * offset for paginating results from
  952. * GET requests to the iContactPro API
  953. * @access public
  954. * @param integer $iOffset
  955. * @return iContactProApi $this
  956. **/
  957. public function setOffset($iOffset) {
  958. // Set the offset in the search parameters
  959. $this->aSearchParameters['offset'] = (integer) $iOffset;
  960. // Return instance
  961. return $this;
  962. }
  963. /**
  964. * This method allows the profileId specified in the configs
  965. * to be overridden
  966. *
  967. * @access public
  968. * @param integer [$iProfileId]
  969. */
  970. public function setProfileId($iProfileId = null)
  971. {
  972. if (!empty ($iProfileId)) {
  973. $this->iProfileId = (integer) $iProfileId;
  974. } else {
  975. $this->iProfileId = (integer) (!empty($this->aConfig['profileId']) ? $this->aConfig['profileId'] : constant('ICP_PROFILEID'));
  976. }
  977. }
  978. }