Ontraport.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace OntraportAPI;
  3. require_once("APIAutoloader.php");
  4. /**
  5. * Class Ontraport
  6. *
  7. * @author ONTRAPORT
  8. *
  9. * @package OntraportAPI
  10. */
  11. class Ontraport
  12. {
  13. /**
  14. * @var string the base URL HTTP requests are made to
  15. */
  16. const REQUEST_URL = "https://api.ontraport.com";
  17. /**
  18. * @var int the API version number for this wrapper
  19. */
  20. const API_VERSION = 1;
  21. /**
  22. * @var string unique site id for API
  23. */
  24. private $_siteID;
  25. /**
  26. * @var int unique API key
  27. */
  28. private $_apiKey;
  29. /**
  30. * @var array contains all instantiated object APIs so multiple instances can be avoided
  31. */
  32. protected $_apiInstances = array();
  33. /**
  34. * @var array contains list of current custom objects so they don't need to be searched for more than once
  35. */
  36. protected $_customObjects = array();
  37. /**
  38. * @var CurlClient instance
  39. */
  40. protected $_httpClient = NULL;
  41. /**
  42. * @brief constructs an instance of OntraportAPI
  43. *
  44. * @param string $siteID
  45. * @param string $apiKey
  46. */
  47. public function __construct($siteID, $apiKey, $httpClient = null)
  48. {
  49. $this->setCredentials($apiKey, $siteID);
  50. $this->setHttpClient($httpClient);
  51. }
  52. /**
  53. * @brief sets API key credential
  54. *
  55. * @param string $apiKey
  56. */
  57. public function setCredentials($apiKey, $siteID)
  58. {
  59. $this->_apiKey = $apiKey;
  60. $this->_siteID = $siteID;
  61. }
  62. /**
  63. * @brief sets HTTP client
  64. */
  65. public function setHttpClient($httpClient = null)
  66. {
  67. if ($httpClient === null)
  68. {
  69. $this->_httpClient = new CurlClient($this->_apiKey, $this->_siteID);
  70. return;
  71. }
  72. $this->_httpClient = $httpClient;
  73. }
  74. /**
  75. * @brief gets HTTP client
  76. */
  77. public function getHttpClient()
  78. {
  79. return $this->_httpClient;
  80. }
  81. /**
  82. * @brief Make an HTTP request
  83. *
  84. * @param $requestParams
  85. * @param string $url
  86. * @param string $method
  87. * @param array $requiredParams
  88. * @param array $options
  89. *
  90. * @return mixed
  91. */
  92. public function request($requestParams, $url, $method, $requiredParams, $options)
  93. {
  94. $client = $this->getHttpClient();
  95. $url = $this->buildEndpoint($url);
  96. return $client->httpRequest($requestParams, $url, $method, $requiredParams, $options);
  97. }
  98. /**
  99. * @brief gets the last HTTP status code received by the HTTP Client
  100. *
  101. * @return int
  102. */
  103. public function getLastStatusCode()
  104. {
  105. return $this->getHttpClient()->getLastStatusCode();
  106. }
  107. /**
  108. * @brief constructs an api endpoint
  109. *
  110. * @param string $extendURL
  111. * @param string $function
  112. *
  113. * @return string
  114. */
  115. public function buildEndpoint($extendURL)
  116. {
  117. return self::REQUEST_URL . "/" . self::API_VERSION . "/" . $extendURL;
  118. }
  119. /**
  120. * @param integer $object
  121. *
  122. * @return CustomObjects instance
  123. *
  124. * @throws Exceptions\CustomObjectException
  125. */
  126. public function custom($object)
  127. {
  128. if (empty($this->_customObjects))
  129. {
  130. $this->_customObjects = $this->object()->retrieveCustomObjects();
  131. }
  132. if (array_key_exists($object, $this->_customObjects))
  133. {
  134. return $this->getApi("CustomObjects", $object);
  135. }
  136. throw new Exceptions\CustomObjectException();
  137. }
  138. /**
  139. * @return CampaignBuilderItems
  140. */
  141. public function campaignbuilder()
  142. {
  143. return $this->getApi("CampaignBuilderItems");
  144. }
  145. /**
  146. * @return Contacts
  147. */
  148. public function contact()
  149. {
  150. return $this->getApi("Contacts");
  151. }
  152. /**
  153. * @return CreditCards
  154. */
  155. public function creditcard()
  156. {
  157. return $this->getApi("CreditCards");
  158. }
  159. /**
  160. * @return Forms
  161. */
  162. public function form()
  163. {
  164. return $this->getApi("Forms");
  165. }
  166. /**
  167. * @return LandingPages
  168. */
  169. public function landingpage()
  170. {
  171. return $this->getApi("LandingPages");
  172. }
  173. /**
  174. * @return Messages
  175. */
  176. public function message()
  177. {
  178. return $this->getApi("Messages");
  179. }
  180. /**
  181. * @return Tasks
  182. */
  183. public function task()
  184. {
  185. return $this->getApi("Tasks");
  186. }
  187. /**
  188. * @return Transactions
  189. */
  190. public function transaction()
  191. {
  192. return $this->getApi("Transactions");
  193. }
  194. /**
  195. * @return Objects
  196. */
  197. public function object()
  198. {
  199. return $this->getApi("Objects");
  200. }
  201. /**
  202. * @return Webhooks
  203. */
  204. public function webhook()
  205. {
  206. return $this->getApi("Webhooks");
  207. }
  208. /**
  209. * @brief if requested API is already instantiated, grabs instance from an array, otherwise autoloads an instance of the API
  210. * @param string $class
  211. * @param integer|null $object
  212. *
  213. * @return mixed
  214. */
  215. public function getApi($class, $object = NULL)
  216. {
  217. $absoluteClass = "\\OntraportAPI\\" . $class;
  218. // For custom objects
  219. if ($object)
  220. {
  221. // Generate a unique name so that wrong object id is not stored with instantiated class
  222. $class = $class . "." . $object;
  223. if (!array_key_exists($class, $this->_apiInstances))
  224. {
  225. $this->_apiInstances[$class] = new $absoluteClass($this, $object);
  226. }
  227. }
  228. else
  229. {
  230. if (!array_key_exists($class, $this->_apiInstances))
  231. {
  232. $this->_apiInstances[$class] = new $absoluteClass($this);
  233. }
  234. }
  235. return $this->_apiInstances[$class];
  236. }
  237. }