CC_Utility

Class that is using REST to communicate with ConstantContact server This class currently supports actions performed using the contacts, lists, and campaigns APIs.

Defined (1)

The class is defined in the following location(s).

/api/cc_class.php  
  1. class CC_Utility { 
  2.  
  3. // FROM HERE YOU MAY MODIFY YOUR CREDENTIALS 
  4. var $login = 'USERNAME'; //Username for your account 
  5. var $password = 'PASSWORD'; //Password for your account 
  6. var $apikey = 'a9f642af-8f34-43b2-8882-00e6aaebfa46'; // KWS API Key. Do not change. 
  7.  
  8. // CONTACT LIST OPTIONS 
  9. var $contact_lists = array(); // Define which lists will be available for sign-up. 
  10. var $force_lists = false; // Set this to true to take away the ability for users to select and de-select lists 
  11. var $show_contact_lists = true; // Set this to false to hide the list name(s) on the sign-up form. 
  12. // NOTE - Contact Lists will only be hidden if force_lists is set to true. This is to prevent available checkboxes form being hidden. 
  13.  
  14. // FORM OPT IN SOURCE - (Who is performing these actions?) 
  15. var $actionBy = 'ACTION_BY_CONTACT'; // Values: ACTION_BY_CUSTOMER or ACTION_BY_CONTACT 
  16. // ACTION_BY_CUSTOMER - Constant Contact Account holder. Used in internal applications. 
  17. // ACTION_BY_CONTACT - Action by Site visitor. Used in web site sign-up forms. 
  18.  
  19. // DEBUGGING 
  20. var $curl_debug = false; // Set this to true to see the response code returned by cURL 
  21.  
  22. // YOUR BASIC CHANGES SHOULD END HERE 
  23. var $requestLogin; //this contains full authentication string. 
  24. var $lastError = ''; // this variable will contain last error message (if any) 
  25. var $apiPath = 'https://api.constantcontact.com/ws/customers/'; //is used for server calls. 
  26. var $doNotIncludeLists = array('Removed', 'Do Not Mail', 'Active'); //define which lists shouldn't be returned. 
  27.  
  28.  
  29. public function __construct() { 
  30. //when the object is getting initialized, the login string must be created as API_KEY%LOGIN:PASSWORD 
  31. $this->requestLogin = $this->apikey."%".$this->login.":".$this->password; 
  32. $this->apiPath = $this->apiPath.$this->login; 
  33.  
  34. /** 
  35. * Method that returns a list with all states found in states.txt file 
  36. * @return array with state codes and state names 
  37. */ 
  38. public function getStates() { 
  39. $returnArr = array(); 
  40. $lines = file("states.txt"); 
  41. foreach ($lines as $line) { 
  42. $tmp = explode(" - ", $line); 
  43. if (sizeof($tmp) == 2) { 
  44. $returnArr[trim($tmp[1])] = trim($tmp[0]); 
  45. return $returnArr; 
  46.  
  47. /** 
  48. * Returns a list with all countries found in countries.txt file 
  49. * @return array with country codes and country names 
  50. */ 
  51. public function getCountries() { 
  52. $returnArr = array(); 
  53. $lines = file("countries.txt"); 
  54. foreach ($lines as $line) { 
  55. $tmp = explode(" - ", $line); 
  56. if (sizeof($tmp) == 2) { 
  57. $returnArr[trim($tmp[1])] = trim($tmp[0]); 
  58. return $returnArr; 
  59.  
  60. /** 
  61. * Validate an email address 
  62. * @return TRUE if address is valid and FALSE if not. 
  63. */ 
  64. protected function isValidEmail($email) { 
  65. return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2, 3})$", $email); 
  66.  
  67. /** 
  68. * Private function used to send requests to ConstantContact server 
  69. * @param string $request_url - is the URL where the request will be made 
  70. * @param string $parameter - if it is not empty then this parameter will be sent using POST method 
  71. * @param string $type - GET/POST/PUT/DELETE 
  72. * @return string server output/response 
  73. */ 
  74. protected function doServerCall($request_url, $parameter = '', $type = "GET") { 
  75.  
  76. $request_url = str_replace('http://', 'https://', $request_url); 
  77.  
  78. $request = array( 
  79. 'method' => $type,  
  80. 'sslverify' => false, // don't screw up with SSL requests 
  81. 'timeout' => 10,  
  82. 'user-agent' => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",  
  83. 'headers' => array( 
  84. 'Authorization' => 'Basic ' . base64_encode( $this->requestLogin ),  
  85. 'Content-Type' => 'application/atom+xml',  
  86. 'Content-Length' => strlen($parameter) 
  87. ),  
  88. 'body' => $parameter,  
  89. ); 
  90.  
  91. $response = wp_remote_request( $request_url, $request ); 
  92.  
  93. if( empty( $response ) || is_wp_error( $response ) ) { 
  94. return false; 
  95. } else if ( wp_remote_retrieve_response_code( $response ) > 299 ) { 
  96. return false; 
  97. } else { 
  98. return wp_remote_retrieve_body( $response ); 
  99.  
  100.