{"id":1735,"date":"2012-08-08T12:52:57","date_gmt":"2012-08-08T12:52:57","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=1735"},"modified":"2014-05-13T05:24:52","modified_gmt":"2014-05-13T05:24:52","slug":"integrate-the-constant-contact-api-with-your-php-application","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2012\/08\/08\/integrate-the-constant-contact-api-with-your-php-application\/","title":{"rendered":"Integrate the Constant Contact API with your PHP application"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPhp.js\"><\/script>\n<figure id=\"attachment_1741\" aria-describedby=\"caption-attachment-1741\" style=\"width: 284px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/truelogic.org\/wordpress\/2012\/08\/08\/integrate-the-constant-contact-api-with-your-php-application\/cc\/\" rel=\"attachment wp-att-1741\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1741\" title=\"cc\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2012\/08\/cc.jpeg\" alt=\"\" width=\"284\" height=\"177\" \/><\/a><figcaption id=\"caption-attachment-1741\" class=\"wp-caption-text\">Constant Contact<\/figcaption><\/figure>\n<h3>WHAT IS CONSTANT CONTACT?<\/h3>\n<p><a href=\"http:\/\/www.constantcontact.com\/index.jsp\" target=\"_blank\">Constant Contact<\/a> is an online service for creating social and email campaigns which is widely used by a lot of websites. It provides a developer API which handles subscription lists and create\/send newsletters.<\/p>\n<p>In this article we will focus on the basics of creating contact lists and creating email campaigns. We will not go into Event Marketing and Bulk Imports\/Exports. This article serves as a quick and simple way of integrating the Constant Contact AppConnect API with your PHP application.<\/p>\n<h3>FIRST STEP<\/h3>\n<p>The first step requires you to have an API Key.\u00a0 The API key is a unique identifier for a Developer, not a customer, and they recommend having one API Key for each integration that you build.\u00a0 So if you have two software platforms and you integrate both, you should create two API keys.<\/p>\n<p>To create an API Key,\u00a0 you need\u00a0 a Constant Contact account and fill out their<a href=\"http:\/\/community.constantcontact.com\/t5\/Documentation\/API-Keys\/ba-p\/25015\" target=\"_blank\"> API Key request form.<\/a><\/p>\n<p>The Constant Contact APIs allow you to manage contacts in your account in various ways.\u00a0 By utilizing contacts collection and resource, you can add, edit, unsubscribe contacts as well as modify their contact list memberships. The Constant Contact REST API contains several useful methods to enable developers to easily manage contacts.<\/p>\n<p><a href=\"http:\/\/community.constantcontact.com\/t5\/Documentation\/API-Documentation\/ba-p\/25125\" target=\"_blank\">To see their complete API docs, click here.<\/a><\/p>\n<p>Before you can start writing code, you need the PHP wrapper classes for this API available on Sourceforge: <a href=\"http:\/\/sourceforge.net\/projects\/ctctphplib\/\" target=\"_blank\">http:\/\/sourceforge.net\/projects\/ctctphplib\/<\/a><\/p>\n<p>Include the classes from this in all your code.<\/p>\n<h3>ADDING\/UPDATING A CONTACT<\/h3>\n<p><pre class=\"brush: php\">$apikey = &#039;YOUR CONSTANT CONTACT APIKEY&#039;;\r\n$cc_username =  &#039;YOUR CONSTANT CONTACT USERNAME;\r\n$cc_password =   &#039;YOUR CONSTANT CONTACT PASSWORD;\r\n\r\n\/\/constructor for constant contact class\r\n$Datastore = new CTCTDataStore();\r\n\r\n$ConstantContact = new ConstantContact(&#039;basic&#039;, $apikey, $cc_username,$cc_password);\r\n$lists = $ConstantContact-&gt;getLists();\r\n\r\n$nextLists = &quot;&quot;;\r\ndo {\r\nif ($nextLists != &quot;&quot;) \r\n{\r\n\t\t$Lists = $nextLists;\r\n\t\t$nextLists = $ConstantContact-&gt;getLists( $Lists [&#039;nextLink&#039;] );\r\n\t} \r\n\telse if ($nextLists == &quot;&quot;) \r\n\t{\r\n\t\t$nextLists = $ConstantContact-&gt;getLists ();\t\r\n\t\t$Lists = $nextLists;\r\n\t}\r\n\t\t\r\n\tforeach ( $Lists [&#039;lists&#039;] as $list ) \r\n\t{\r\n\t\tif($list-&gt;name == &#039;YOUR LIST NAME&#039;)\r\n\t\t$cc_daily_deals_list_id = $list-&gt;id;\t\t\t\t\t\t}\r\n} while ( $Lists [&#039;nextLink&#039;] != false );\r\n\r\n$search = $ConstantContact-&gt;searchContactsByEmail($emailId);\r\nif($search == false)\r\n{\r\n\/\/ adding contact in constant contact in both the list\r\n\t$Contact = new Contact();\r\n\t$Contact-&gt;emailAddress =&#039;EMAIL ID TO SUBSCRIBE&#039;;\r\n\t$Contact-&gt;City = &#039;CITY&#039;;\r\n\t$Contact-&gt;lists = array($cc_daily_deals_list_id);\t\t\t\t\r\n$Contact-&gt;CountryCode = &#039;COUNTRY_CODE&#039;;\r\n$NewContact = $ConstantContact-&gt;addContact($Contact);\t\t\t\r\n}\r\nelse\r\n{\t\r\n$Contact = $ConstantContact-&gt;getContactDetails($search[0]);\r\nif($Contact-&gt;status != &quot;Do Not Mail&quot;)\t\r\n{\r\n$Contact-&gt;City = &#039;CITY&#039;;\r\n$Contact-&gt;CountryCode = &#039;COUNTRY_CODE&#039;;\r\n\t\tif(!in_array($cc_daily_deals_list_id,$Contact-&gt;lists))\r\n\t\t\tarray_push($Contact-&gt;lists,$cc_daily_deals_list_id);\t\t\t\t\t\t\t\t\t\t\r\n\t\tif($Contact-&gt;status == &quot;Removed&quot;)\t\t\t\t\t\t\t\tif(!in_array($cc_daily_deals_list_id,$Contact-&gt;lists))\r\n\t\t\t$Contact-&gt;lists = array($cc_daily_deals_list_id);\t\t\t\r\nif(!in_array($cc_daily_deals_list_id,$Contact-&gt;lists))\r\n\t\t\tarray_push($Contact-&gt;lists, $cc_daily_deals_list_id);\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\/\/ Update the contact and DONE\r\n\t\t$NewContact = $ConstantContact-&gt;updateContact($Contact);\t\t  }\r\n  }\t\t\t\r\n<\/pre><\/p>\n<h3>CREATE AN EMAIL\u00a0 CAMPAIGN<\/h3>\n<p><pre class=\"brush: php\">1)\tCreate Campaign\r\n$Datastore = new CTCTDataStore ();\r\n$ConstantContact = new ConstantContact(&#039;basic&#039;, $apikey, $cc_username,$cc_password);\r\n\r\n$lists = $ConstantContact-&gt;getLists();\t\r\n\t\t\t\r\n\/\/ Get all verified email addresses\r\n$VerifiedEmailAddresses = $ConstantContact-&gt;getVerifiedAddresses ();\r\n\r\n\/\/ getting the list id to send campaign to\r\n$nextLists = &quot;&quot;;\r\ndo \t{\r\n\tif ($nextLists != &quot;&quot;) \r\n\t{\r\n\t\t$Lists = $nextLists;\t\t\t\t\t\t\t\t\t$nextLists = $ConstantContact-&gt;getLists( $Lists [&#039;nextLink&#039;] );\r\n\t} \r\n\telse if ($nextLists == &quot;&quot;) \r\n\t{\r\n\t\t$nextLists = $ConstantContact-&gt;getLists ();\t\t\t\t\t\t$Lists = $nextLists;\r\n\t}\r\n\tforeach ( $Lists [&#039;lists&#039;] as $list ) \r\n{\r\n\t\t\/\/exit(var_dump($list));\r\n\t\tif($list-&gt;name == $cc_list_name)\r\n\t\t\t$cc_daily_deals_list_id = $list-&gt;id;\t\t\t\t\r\n\t\t}\r\n\t\t\t\t\r\n } while ( $Lists [&#039;nextLink&#039;] != false );\r\n\r\n\/\/ email content\r\n$ch = curl_init();\r\n$timeout = 400000;\r\ncurl_setopt($ch, CURLOPT_TIMEOUT, 200000);\r\ncurl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);\r\ncurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);\r\n\r\n \/\/ Getting binary data\r\n  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\n  curl_setopt ($ch, CURLOPT_URL, &ldquo;YOUR PAGE UR&rdquo;);\r\n $codeString = curl_exec($ch);\r\n curl_close($ch);\r\n\t\t\t\t\t\t\r\n$codeString = str_replace(&quot;&pound;&quot;,&quot;&amp;pound;&quot;,$codeString);\r\n$codeString = str_replace(&quot;&euro;&quot;,&quot;&amp;euro;&quot;,$codeString);\r\n$codeString = str_replace(&#039;&lt;!DOCTYPE HTML PUBLIC &quot;-\/\/W3C\/\/DTD HTML 4.0 strict\/\/EN&quot;&gt;&#039;,&quot;&quot;,$codeString);\r\n\/\/$codeString = substr($codeString,0,75732);\r\n\r\n\/\/ creating campaign starts here\t\r\ntry {\r\n\t$campaign_name = &quot;Campaign For &quot;.ucfirst($city_name).&quot; on &quot;.date(&#039;Y-m-d h:i:s&#039;);\t\/\/Build Campaign Object\r\n\t$myCampaign = new Campaign();\t\t\t\t\t\t\t$myCampaign-&gt;name = $campaign_name;\r\n\t$myCampaign-&gt;subject = ucfirst($city_name).&quot;: &quot;.$mail_subject;\r\n\t$myCampaign-&gt;fromName = $from_name;\t\r\n\t\t\t\t\t\t\t\r\n\tif ($from_email != &quot;&quot;) \r\n\t{\r\n\t\t\/\/ Do while is necessary to grab the next 50 results and build\r\n\t\t\/\/ the next request so it keeps iterating until completion\r\n\tdo {\r\n\t\t\/\/ Gets the next link if there are more than 50 results to\r\n\t\t\/\/ be returned\r\n$moreVerifiedEmailAddresses = $ConstantContact-&gt;getVerifiedAddresses ( $VerifiedEmailAddresses [&#039;nextLink&#039;] );\r\n\tforeach ( $VerifiedEmailAddresses [&#039;addresses&#039;] as $VerifiedEmailAddress ) {\r\n\t\tif ($VerifiedEmailAddress-&gt;email == $from_email) {\r\n\t\t\t$myCampaign-&gt;fromAddress = $VerifiedEmailAddress;\t\t\t\t\t$myCampaign-&gt;replyAddress = $VerifiedEmailAddress;\t\t\t\t\t$fromAddress = $VerifiedEmailAddress;\r\n\t\t\tbreak; \/\/ We found our email address so we can exit our\r\n\t  \/\/ foreach\r\n\t}\t\r\n\t}\r\n\t\/\/ Sets original Verified emaillAddresses to the next 50 so\r\n\t\/\/ it will replace its values\r\n\t$VerifiedEmailAddresses = $moreVerifiedEmailAddresses;\r\n\t} while ( $VerifiedEmailAddresses [&#039;nextLink&#039;] != false );\r\n}\r\n\r\n\t$myCampaign-&gt;greetingName = &quot;NONE&quot;;\r\n\t$myCampaign-&gt;greetingSalutation = &quot;DEAR&quot;;\r\n\t$myCampaign-&gt;greetingString = &quot;&quot;;\r\n\t$myCampaign-&gt;orgName = $orgn_name;\r\n\t$myCampaign-&gt;orgAddr1 = $address;\r\n\t$myCampaign-&gt;orgAddr2 = &quot;&quot;;\r\n\t$myCampaign-&gt;orgAddr3 = &quot;&quot;;\r\n$myCampaign-&gt;orgCity = $cc_city;\r\n\t$myCampaign-&gt;orgState = $cc_state;\r\n\t\r\n\/\/ Validates that if the user selects United states, the stateOther\r\n\/\/ is empty\r\nif ($cc_country == &quot;United States&quot;) {\r\n\t\t$stateOther == NULL;\r\n}\r\n\t$myCampaign-&gt;orgInternationalState = NULL;\r\n$myCampaign-&gt;orgPostalCode = $cc_zipCode;\r\n\t$myCampaign-&gt;orgCountry = $cc_country;\t\t\t\t\t\t$myCampaign-&gt;incForwardEmail = &quot;NO&quot;;\r\n\t$myCampaign-&gt;incSubscribeLink = &quot;NO&quot;;\r\n\t$myCampaign-&gt;forwardEmailLinkText = &quot;&quot;;\r\n\t$myCampaign-&gt;subscribeLinkText = &quot;&quot;;\r\n\t$myCampaign-&gt;emailContentFormat = &quot;HTML&quot;;\r\n$myCampaign-&gt;emailContent = htmlspecialchars($codeString);\r\n\t$myCampaign-&gt;textVersionContent = &quot;&lt;text&gt;This is the text version&lt;\/text&gt;&quot;;\r\n\t$myCampaign-&gt;lists = array($cc_daily_deals_list_id);\r\n\t\/\/print_r($myCampaign-&gt;emailContent);\r\n\t\/\/exit;\r\n$CampaignResult = $ConstantContact-&gt;addCampaign($myCampaign, $fromAddress);\r\n\t\/\/print_r($CampaignResult);\r\n\t\/\/exit;\r\n\t$schedule_time = date(&quot;Y-m-d&quot;).&quot; &quot;.$local_time;\t\t\t\t\r\n\t\t\t\t\t\t\t$obj=new convert_timezone;\r\n\t\t\t\t\t\t\t$str=$obj-&gt;conver_to_time($country_timezone,$schedule_time,0);\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\techo &quot;New Campaign created for &quot; .$city_name .&quot; ID:&quot;.$CampaignResult-&gt;id.&quot;\\n&lt;br \/&gt;&quot;;\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t$lastCampaigns = $ConstantContact-&gt;getCampaigns();\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\tif (isset ( $CampaignResult-&gt;status )) {\r\n\t\t\t\t\t\t\t\tforeach ( $lastCampaigns [&#039;campaigns&#039;] as $recent ) {\r\n\t\t\t\t\t\t\t\t\tif ($recent-&gt;name == $CampaignResult-&gt;name) {\r\n\t\t\t\t\t\t\t\t\t\t$CampaignResult = $ConstantContact-&gt;getCampaignDetails ( $recent );\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\/\/ schedule campaign here\r\n\t\t\t\t\t\t\t$schedule_flag = $ConstantContact-&gt;scheduleCampaign($CampaignResult,date(&#039;c&#039;,strtotime($str)));\r\n\t\t\t\t\t\t\tif(isset($schedule_flag) &amp;&amp; $schedule_flag != NULL &amp;&amp; $schedule_flag != &quot;&quot;){\r\n\t\t\t\t\t\t\t\techo &quot;Campaign is scheduled at &quot;.$str.&quot;&lt;br \/&gt;&lt;br \/&gt;&quot;;\r\n<\/pre><\/p>\n<h3>UNSUBSCRIBE CONTACT FROM A LIST<\/h3>\n<p><pre class=\"brush: php\">$search = $ConstantContact-&gt;searchContactsByEmail(&#039;OUR EMAIL TO UNSUBSCRIBE&#039;;\r\n$Contact = $ConstantContact-&gt;getContactDetails($search[0]);\r\nif(in_array($list_id, $Contact-&gt;lists))\r\n$Contact-&gt;lists = array_values(array_diff($Contact-&gt;lists, array($list_id)));\r\n$NewContact = $ConstantContact-&gt;updateContact($Contact);\r\n<\/pre><\/p>\n<p>The above are the basic operations which will be used. For more details, its best to consult the API documentation from the link given above.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>WHAT IS CONSTANT CONTACT? Constant Contact is an online service for creating social and email campaigns which is widely used by a lot of websites. <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2012\/08\/08\/integrate-the-constant-contact-api-with-your-php-application\/\" title=\"Integrate the Constant Contact API with your PHP application\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":1741,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1735","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apachephp"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/1735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=1735"}],"version-history":[{"count":10,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/1735\/revisions"}],"predecessor-version":[{"id":2042,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/1735\/revisions\/2042"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/1741"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=1735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=1735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=1735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}