csrest_general.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. require_once dirname(__FILE__).'/class/base_classes.php';
  3. /**
  4. * Class to access general resources from the create send API.
  5. * @author tobyb
  6. *
  7. */
  8. class CS_REST_General extends CS_REST_Wrapper_Base {
  9. /**
  10. * Get the authorization URL for your application, given the application's
  11. * Client ID, Client Secret, Redirect URI, Scope, and optional state data.
  12. *
  13. * @param $client_id int The Client ID of your registered OAuth application.
  14. * @param $redirect_uri string The Redirect URI of your registered OAuth application.
  15. * @param $scope string The comma-separated permission scope your application requires.
  16. * See http://www.campaignmonitor.com/api/getting-started/#authenticating_with_oauth for details.
  17. * @param $state string Optional state data to be included in the URL.
  18. * @return string The authorization URL to which users of your application should be redirected.
  19. * @access public
  20. **/
  21. public static function authorize_url(
  22. $client_id, $redirect_uri, $scope, $state = NULL) {
  23. $qs = "client_id=".urlencode($client_id);
  24. $qs .= "&redirect_uri=".urlencode($redirect_uri);
  25. $qs .= "&scope=".urlencode($scope);
  26. if ($state) {
  27. $qs .= "&state=".urlencode($state);
  28. }
  29. return CS_OAUTH_BASE_URI.'?'.$qs;
  30. }
  31. /**
  32. * Exchange a provided OAuth code for an OAuth access token, 'expires in'
  33. * value and refresh token.
  34. *
  35. * @param $client_id int The Client ID of your registered OAuth application.
  36. * @param $client_secret string The Client Secret of your registered OAuth application.
  37. * @param $redirect_uri string The Redirect URI of your registered OAuth application.
  38. * @param $code string The unique OAuth code to be exchanged for an access token.
  39. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  40. * {
  41. * 'access_token' => The access token to use for API calls
  42. * 'expires_in' => The number of seconds until this access token expires
  43. * 'refresh_token' => The refresh token to refresh the access token once it expires
  44. * }
  45. * @access public
  46. **/
  47. public static function exchange_token(
  48. $client_id, $client_secret, $redirect_uri, $code) {
  49. $body = "grant_type=authorization_code";
  50. $body .= "&client_id=".urlencode($client_id);
  51. $body .= "&client_secret=".urlencode($client_secret);
  52. $body .= "&redirect_uri=".urlencode($redirect_uri);
  53. $body .= "&code=".urlencode($code);
  54. $options = array('contentType' => 'application/x-www-form-urlencoded');
  55. $wrap = new CS_REST_Wrapper_Base(
  56. NULL, 'https', CS_REST_LOG_NONE, CS_HOST, NULL,
  57. new CS_REST_DoNothingSerialiser(), NULL);
  58. return $wrap->post_request(CS_OAUTH_TOKEN_URI, $body, $options);
  59. }
  60. /**
  61. * Constructor.
  62. * @param $auth_details array Authentication details to use for API calls.
  63. * This array must take one of the following forms:
  64. * If using OAuth to authenticate:
  65. * array(
  66. * 'access_token' => 'your access token',
  67. * 'refresh_token' => 'your refresh token')
  68. *
  69. * Or if using an API key:
  70. * array('api_key' => 'your api key')
  71. * @param $protocol string The protocol to use for requests (http|https)
  72. * @param $debug_level int The level of debugging required CS_REST_LOG_NONE | CS_REST_LOG_ERROR | CS_REST_LOG_WARNING | CS_REST_LOG_VERBOSE
  73. * @param $host string The host to send API requests to. There is no need to change this
  74. * @param $log CS_REST_Log The logger to use. Used for dependency injection
  75. * @param $serialiser The serialiser to use. Used for dependency injection
  76. * @param $transport The transport to use. Used for dependency injection
  77. * @access public
  78. */
  79. function CS_REST_Wrapper_Base(
  80. $auth_details,
  81. $protocol = 'https',
  82. $debug_level = CS_REST_LOG_NONE,
  83. $host = 'api.createsend.com',
  84. $log = NULL,
  85. $serialiser = NULL,
  86. $transport = NULL) {
  87. $this->CS_REST_Wrapper_Base($auth_details, $protocol, $debug_level, $host, $log, $serialiser, $transport);
  88. }
  89. /**
  90. * Gets an array of valid timezones
  91. * @access public
  92. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  93. * array<string>(timezones)
  94. */
  95. function get_timezones() {
  96. return $this->get_request($this->_base_route.'timezones.json');
  97. }
  98. /**
  99. * Gets the current date in your accounts timezone
  100. * @access public
  101. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  102. * {
  103. * 'SystemDate' => string The current system date in your accounts timezone
  104. * }
  105. */
  106. function get_systemdate() {
  107. return $this->get_request($this->_base_route.'systemdate.json');
  108. }
  109. /**
  110. * Gets an array of valid countries
  111. * @access public
  112. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  113. * array<string>(countries)
  114. */
  115. function get_countries() {
  116. return $this->get_request($this->_base_route.'countries.json');
  117. }
  118. /**
  119. * Gets an array of clients
  120. * @access public
  121. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  122. * array(
  123. * {
  124. * 'ClientID' => The clients API ID,
  125. * 'Name' => The clients name
  126. * }
  127. * )
  128. */
  129. function get_clients() {
  130. return $this->get_request($this->_base_route.'clients.json');
  131. }
  132. /**
  133. * Gets your billing details.
  134. * @access public
  135. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  136. * {
  137. * 'Credits' => The number of credits belonging to the account
  138. * }
  139. */
  140. function get_billing_details() {
  141. return $this->get_request($this->_base_route.'billingdetails.json');
  142. }
  143. /**
  144. * Gets an array of administrators
  145. * @access public
  146. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  147. * array(
  148. * {
  149. * 'EmailAddress' => The administrators email address
  150. * 'Name' => The administrators name
  151. * 'Status' => The administrators status
  152. * }
  153. * )
  154. */
  155. function get_administrators() {
  156. return $this->get_request($this->_base_route.'admins.json');
  157. }
  158. /**
  159. * Retrieves the email address of the primary contact for this account
  160. * @return CS_REST_Wrapper_Result a successful response will be an array in the form:
  161. * array('EmailAddress'=> email address of primary contact)
  162. */
  163. function get_primary_contact() {
  164. return $this->get_request($this->_base_route.'primarycontact.json');
  165. }
  166. /**
  167. * Assigns the primary contact for this account to the administrator with the specified email address
  168. * @param $emailAddress string The email address of the administrator designated to be the primary contact
  169. * @return CS_REST_Wrapper_Result a successful response will be an array in the form:
  170. * array('EmailAddress'=> email address of primary contact)
  171. */
  172. function set_primary_contact($emailAddress) {
  173. return $this->put_request($this->_base_route.'primarycontact.json?email=' . urlencode($emailAddress), '');
  174. }
  175. /**
  176. * Get a URL which initiates a new external session for the user with the given email.
  177. * Full details: http://www.campaignmonitor.com/api/account/#single_sign_on
  178. *
  179. * @param $session_options array Options for initiating the external login session.
  180. * This should be an array of the form:
  181. * array(
  182. * 'Email' => 'The email address of the Campaign Monitor user for whom the login session should be created',
  183. * 'Chrome' => 'Which 'chrome' to display - Must be either "all", "tabs", or "none"',
  184. * 'Url' => 'The URL to display once logged in. e.g. "/subscribers/"',
  185. * 'IntegratorID' => 'The Integrator ID. You need to contact Campaign Monitor support to get an Integrator ID.',
  186. * 'ClientID' => 'The Client ID of the client which should be active once logged in to the Campaign Monitor account.' )
  187. *
  188. * @return CS_REST_Wrapper_Result A successful response will be an array of the form:
  189. * array('SessionUrl'=> 'https://external1.createsend.com/cd/create/ABCDEF12/DEADBEEF?url=FEEDDAD1')
  190. */
  191. function external_session_url($session_options) {
  192. return $this->put_request($this->_base_route.'externalsession.json', $session_options);
  193. }
  194. }