Objects.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. namespace OntraportAPI;
  3. /**
  4. * Class Objects
  5. *
  6. * @author ONTRAPORT
  7. *
  8. * @package OntraportAPI
  9. */
  10. class Objects extends BaseApi
  11. {
  12. /**
  13. * $var string endpoint for single object
  14. */
  15. protected $_endpoint = "object";
  16. /**
  17. * $var string endpoint for plural object
  18. */
  19. protected $_endpointPlural = "objects";
  20. /**
  21. * @param Ontraport $client
  22. */
  23. public function __construct(Ontraport $client)
  24. {
  25. parent::__construct($client);
  26. }
  27. // Object-specific function endpoints
  28. const TAG = "tag";
  29. const TAG_BY_NAME = "tagByName";
  30. const SEQUENCE = "sequence";
  31. const PAUSE = "pause";
  32. const UNPAUSE = "unpause";
  33. const SUBSCRIBE = "subscribe";
  34. const GET_BY_EMAIL = "getByEmail";
  35. /**
  36. * @brief Retrieve a single specified object
  37. *
  38. * @param $requestParams mixed[] The parameters to submit with GET request.
  39. * Possible array keys: "id" (required), "objectID" (required)
  40. *
  41. * @return string JSON formatted response
  42. */
  43. public function retrieveSingle($requestParams)
  44. {
  45. $requiredParams = array(
  46. "id",
  47. "objectID"
  48. );
  49. return $this->client->request($requestParams, $this->_endpoint, "get", $requiredParams, $options = NULL);
  50. }
  51. /**
  52. * @brief Retrieve multiple objects according to specific criteria, handle pagination
  53. *
  54. * @param $requestParams mixed[] Array of parameters to submit with GET request. If "ids"
  55. * are not specified, all will be selected.
  56. * Possible array keys: "objectID" (required),"ids","start","range","sort","sortDir",
  57. * "condition","search","searchNotes","group_ids","performAll",
  58. * "externs","listFields"
  59. *
  60. * @return string JSON formatted array of response data: each page of data will be an element in that array.
  61. */
  62. public function retrieveMultiplePaginated($requestParams)
  63. {
  64. $collection = json_decode($this->retrieveCollectionInfo($requestParams), true);
  65. $requestParams["start"] = $requestParams["start"] ?: 0;
  66. $requestParams["range"] = $requestParams["range"] ?: 50;
  67. $object_data = array();
  68. while ($requestParams["start"] < $collection["data"]["count"])
  69. {
  70. $object_data[] = json_decode($this->retrieveMultiple($requestParams), true);
  71. $requestParams["start"] += $requestParams["range"];
  72. }
  73. return json_encode($object_data);
  74. }
  75. /**
  76. * @brief Retrieve multiple objects according to specific criteria
  77. *
  78. * @param $requestParams mixed[] Array of parameters to submit with GET request. If "ids"
  79. * are not specified, all will be selected.
  80. * Possible array keys: "objectID" (required),"ids","start","range","sort","sortDir",
  81. * "condition","search","searchNotes","group_ids","performAll",
  82. * "externs","listFields"
  83. *
  84. * @return string JSON formatted response
  85. */
  86. public function retrieveMultiple($requestParams)
  87. {
  88. $requiredParams = array(
  89. "objectID"
  90. );
  91. return $this->client->request($requestParams, $this->_endpointPlural, "get", $requiredParams , $options = NULL);
  92. }
  93. /**
  94. * @brief Retrieve all objects of type having a particular tag
  95. *
  96. * @param $requestParams mixed[] Array of parameters to submit with GET request. Either "tag_id" or "tag_name" is required.
  97. * Possible array keys: "objectID" (required),"tag_id","tag_name","start","range","sort","sortDir",
  98. * "condition","search","searchNotes","group_ids","performAll",
  99. * "externs","listFields"
  100. *
  101. * @return string JSON formatted response
  102. */
  103. public function retrieveAllWithTag($requestParams)
  104. {
  105. $requiredParams = array(
  106. "objectID"
  107. );
  108. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::TAG, "get", $requiredParams , $options = NULL);
  109. }
  110. /**
  111. * @brief Retrieves object ID by email.
  112. *
  113. * @param $requestParams mixed[] Array of parameters to submit with GET request.
  114. * Possible array keys: "objectID" (required),"email" (required)
  115. *
  116. * @return string JSON formatted response
  117. */
  118. public function retrieveIdByEmail($requestParams)
  119. {
  120. $requiredParams = array(
  121. "objectID",
  122. "email"
  123. );
  124. return $this->client->request($requestParams, $this->_endpoint . "/" . self::GET_BY_EMAIL, "get", $requiredParams , $options = NULL);
  125. }
  126. /**
  127. * @brief Create an object
  128. *
  129. * @param $requestParams mixed[] Array of parameters to submit with POST request.
  130. * Possible array keys: "objectID" (required), other parameters will vary by object type.
  131. * If you do not know what parameters to use, you may wish to consult our API Reference
  132. * or make a call to getMeta for the object in question. Invalid parameters passed in
  133. * will be ignored.
  134. *
  135. * @return string JSON formatted response
  136. */
  137. public function create($requestParams)
  138. {
  139. $requiredParams = array(
  140. "objectID"
  141. );
  142. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_JSON);
  143. return $this->client->request($requestParams, $this->_endpointPlural, "post", $requiredParams, $options);
  144. }
  145. /**
  146. * @brief Delete a single specified object
  147. *
  148. * @param $requestParams mixed[] Array of the parameters to submit with DELETE request.
  149. * Possible array keys: "id" (required),"objectID" (required)
  150. *
  151. * @return string JSON formatted response
  152. */
  153. public function deleteSingle($requestParams)
  154. {
  155. $requiredParams = array(
  156. "id",
  157. "objectID"
  158. );
  159. return $this->client->request($requestParams, $this->_endpoint, "delete", $requiredParams, $options = NULL);
  160. }
  161. /**
  162. * @brief Delete multiple objects according to specific criteria
  163. *
  164. * @param $requestParams mixed[] Array of parameters to submit with DELETE request. If "ids"
  165. * are not specified, all will be selected.
  166. * Possible array keys: "objectID" (required),"ids","start","range","sort","sortDir",
  167. * "condition","search","searchNotes","group_ids","performAll",
  168. * "externs","listFields"
  169. *
  170. * @return string JSON formatted response
  171. */
  172. public function deleteMultiple($requestParams)
  173. {
  174. $requiredParams = array(
  175. "objectID"
  176. );
  177. return $this->client->request($requestParams, $this->_endpointPlural, "delete", $requiredParams, $options = NULL);
  178. }
  179. /**
  180. * @brief Update an object's data
  181. *
  182. * @param $requestParams mixed[] Array of parameters to submit with PUT request.
  183. * Possible array keys: "objectID" (required), other parameters will vary by object type.
  184. * If you do not know what parameters to use, you may wish to consult our API Reference
  185. * or make a call to getMeta for the object in question. Invalid parameters passed in
  186. * will be ignored.
  187. *
  188. * @return string JSON formatted response
  189. */
  190. public function update($requestParams)
  191. {
  192. $requiredParams = array(
  193. "objectID"
  194. );
  195. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_JSON);
  196. return $this->client->request($requestParams, $this->_endpointPlural, "put", $requiredParams, $options);
  197. }
  198. /**
  199. * @brief Retrieve meta for objects
  200. *
  201. * @param $requestParams mixed[] The parameters to submit with GET request.
  202. * ObjectID is optional but query will return all objects if not included.
  203. * Possible array keys: "format","objectID"
  204. *
  205. * @return string JSON formatted response
  206. */
  207. public function retrieveMeta($requestParams = NULL)
  208. {
  209. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::META, "get", $requiredParams = NULL, $options = NULL);
  210. }
  211. /**
  212. * @brief Either create an object or merge with an existing object on unique field
  213. *
  214. * @param $requestParams mixed[] The parameters to submit with PUT request.
  215. * Possible array keys: "objectID" (required), other parameters will vary by object type.
  216. * If you do not know what parameters to use, you may wish to consult our API Reference
  217. * or make a call to getMeta for the object in question. Invalid parameters passed in
  218. * will be ignored.
  219. *
  220. * @return string JSON formatted response
  221. */
  222. public function saveOrUpdate($requestParams)
  223. {
  224. $requiredParams = array(
  225. "objectID"
  226. );
  227. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_FORM);
  228. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::SAVE_OR_UPDATE, "post", $requiredParams, $options);
  229. }
  230. /**
  231. * @brief Retrieve information (such as number of objects) about object collection
  232. *
  233. * @param $requestParams mixed[] Array of parameters to submit with GET request.
  234. * Possible array keys: "objectID" (required),"condition","search","searchNotes",
  235. * "group_ids","performAll"
  236. *
  237. * @return string JSON formatted response
  238. */
  239. public function retrieveCollectionInfo($requestParams)
  240. {
  241. $requiredParams = array(
  242. "objectID"
  243. );
  244. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::GET_INFO, "get", $requiredParams, $options = NULL);
  245. }
  246. /**
  247. * @brief get an array of all custom objects in the account's database.
  248. *
  249. * @return array
  250. */
  251. public function retrieveCustomObjects()
  252. {
  253. $objects = $this->retrieveMeta($requestParams = NULL);
  254. $objects = json_decode($objects, true);
  255. $objects = $objects["data"];
  256. $customObjects = array();
  257. foreach ($objects as $id => $data)
  258. {
  259. if ($id >= 10000)
  260. {
  261. $customObjects[$id] = $data;
  262. }
  263. }
  264. return $customObjects;
  265. }
  266. /**
  267. * @brief Pause rules, sequences, and sequence subscribers for one or more objects.
  268. *
  269. * @param mixed[] $requestParams Array of parameters to submit with POST request.
  270. * Possible array keys: "objectID" (required),"ids","start","range","sort","sortDir",
  271. * "condition","search","group_ids","performAll","externs","listFields".
  272. * Either "ids" or "group_ids" is required.
  273. *
  274. * @return string JSON formatted HTTP response
  275. */
  276. public function pause($requestParams)
  277. {
  278. $requiredParams = array(
  279. "ids"
  280. );
  281. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_FORM);
  282. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::PAUSE, "post", $requiredParams, $options);
  283. }
  284. /**
  285. * @brief Unpause rules, sequences, and sequence subscribers for one or more objects.
  286. *
  287. * @param mixed[] $requestParams Array of parameters to submit with POST request.
  288. * Possible array keys: "objectID" (required),"ids","start","range","sort","sortDir",
  289. * "condition","search","group_ids","performAll","externs","listFields".
  290. * Either "ids" or "group_ids" is required.
  291. *
  292. * @return string JSON formatted HTTP response
  293. */
  294. public function unpause($requestParams)
  295. {
  296. $requiredParams = array(
  297. "ids"
  298. );
  299. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_FORM);
  300. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::UNPAUSE, "post", $requiredParams, $options);
  301. }
  302. /**
  303. * @brief Remove one or more objects from one or more sequences
  304. *
  305. * @param mixed[] $requestParams Array of parameters to submit with DELETE request.
  306. * Possible array keys: "objectID" (required),"remove_list" (required),"ids","start",
  307. * "range","sort","sortDir","condition","search","searchNotes",
  308. * "group_ids","performAll","externs","listFields".
  309. * Either "ids" or "group_ids" is required.
  310. *
  311. * @return string JSON formatted HTTP response
  312. */
  313. public function removeFromSequence($requestParams)
  314. {
  315. $requiredParams = array(
  316. "remove_list",
  317. "ids"
  318. );
  319. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_FORM);
  320. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::SEQUENCE, "delete", $requiredParams, $options);
  321. }
  322. /**
  323. * @brief Add one or more objects to one or more sequences
  324. *
  325. * @param mixed[] $requestParams Array of parameters to submit with PUT request.
  326. * Possible array keys: "objectID" (required),"remove_list" (required),"ids","start",
  327. * "range","sort","sortDir","condition","search","searchNotes",
  328. * "group_ids","performAll","externs","listFields".
  329. * Either "ids" or "group_ids" is required.
  330. *
  331. * @return string JSON formatted HTTP response
  332. */
  333. public function addToSequence($requestParams)
  334. {
  335. $requiredParams = array(
  336. "add_list",
  337. "ids"
  338. );
  339. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_FORM);
  340. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::SEQUENCE, "put", $requiredParams, $options);
  341. }
  342. /**
  343. * @brief Add one or more tags to one or more objects
  344. *
  345. * @param mixed[] $requestParams Array of parameters to submit with PUT request.
  346. * Possible array keys: "objectID" (required),"add_list" (required),"ids","start",
  347. * "range","sort","sortDir","condition","search","searchNotes",
  348. * "group_ids","performAll","externs","listFields".
  349. * Either "ids" or "group_ids" is required.
  350. *
  351. * @return string JSON formatted HTTP response
  352. */
  353. public function addTag($requestParams)
  354. {
  355. $requiredParams = array(
  356. "add_list",
  357. "ids"
  358. );
  359. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_FORM);
  360. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::TAG, "put", $requiredParams, $options);
  361. }
  362. /**
  363. * @brief Remove one or more tags from one or more objects
  364. *
  365. * @param mixed[] $requestParams Array of parameters to submit with DELETE request.
  366. * Possible array keys: "objectID" (required),"remove_list" (required),"ids","start",
  367. * "range","sort","sortDir","condition","search","searchNotes",
  368. * "group_ids","performAll","externs","listFields".
  369. * Either "ids" or "group_ids" is required.
  370. *
  371. * @return string JSON formatted HTTP response
  372. */
  373. public function removeTag($requestParams)
  374. {
  375. $requiredParams = array(
  376. "remove_list",
  377. "ids"
  378. );
  379. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_FORM);
  380. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::TAG, "delete", $requiredParams, $options);
  381. }
  382. /**
  383. * @brief Add one or more tags to one or more objects by tag name (create tag if it doesn't exist)
  384. *
  385. * @param mixed[] $requestParams Array of parameters to submit with PUT request.
  386. * Possible array keys: "objectID" (required),"add_names" (required),"ids","start",
  387. * "range","sort","sortDir","condition","search","searchNotes",
  388. * "group_ids","performAll","externs","listFields".
  389. * Either "ids" or "group_ids" is required.
  390. *
  391. * @return string JSON formatted HTTP response
  392. */
  393. public function addTagByName($requestParams)
  394. {
  395. $requiredParams = array(
  396. "add_names",
  397. "ids"
  398. );
  399. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_JSON);
  400. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::TAG_BY_NAME, "put", $requiredParams, $options);
  401. }
  402. /**
  403. * @brief Remove one or more tags from one or more objects by tag name
  404. *
  405. * @param mixed[] $requestParams Array of parameters to submit with DELETE request.
  406. * Possible array keys: "objectID" (required),"remove_names" (required),"ids","start",
  407. * "range","sort","sortDir","condition","search","searchNotes",
  408. * "group_ids","performAll","externs","listFields".
  409. * Either "ids" or "group_ids" is required.
  410. *
  411. * @return string JSON formatted HTTP response
  412. */
  413. public function removeTagByName($requestParams)
  414. {
  415. $requiredParams = array(
  416. "remove_names",
  417. "ids"
  418. );
  419. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_JSON);
  420. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::TAG_BY_NAME, "delete", $requiredParams, $options);
  421. }
  422. /**
  423. * @brief Add one or more objects to one or more campaigns or sequences
  424. *
  425. * @param mixed[] $requestParams Array of parameters to submit with PUT request.
  426. * Possible array keys: "objectID" (required),"add_list" (required),"ids",
  427. * "sub_type" (default: campaign), "start","range","sort",
  428. * "sortDir","condition","search","searchNotes","group_ids",
  429. * "performAll","externs","listFields".
  430. *
  431. * @return string JSON formatted HTTP response
  432. */
  433. public function subscribe($requestParams)
  434. {
  435. $requiredParams = array(
  436. "add_list",
  437. "ids"
  438. );
  439. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_FORM);
  440. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::SUBSCRIBE, "put", $requiredParams, $options);
  441. }
  442. /**
  443. * @brief Remove one or more objects from one or more campaigns or sequences
  444. *
  445. * @param mixed[] $requestParams Array of parameters to submit with DELETE request.
  446. * Possible array keys: "objectID" (required),"remove_list" (required),"ids",
  447. * "sub_type" (default: campaign), "start","range","sort",
  448. * "sortDir","condition","search","searchNotes","group_ids",
  449. * "performAll","externs","listFields".
  450. *
  451. * @return string JSON formatted HTTP response
  452. */
  453. public function unsubscribe($requestParams)
  454. {
  455. $requiredParams = array(
  456. "remove_list",
  457. "ids"
  458. );
  459. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_FORM);
  460. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::SUBSCRIBE, "delete", $requiredParams, $options);
  461. }
  462. }