Created
January 30, 2018 17:58
-
-
Save jimfloss/49abb75403aaa2fe648a680d6776cb6a to your computer and use it in GitHub Desktop.
Add Contact to Constant Contact Using cURL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Not original code, just don't want to loose it | |
// fill in your Constant Contact login and API key | |
$ccuser = get_theme_mod( 'ccuser' ); | |
$ccpass = get_theme_mod( 'ccpass' ); | |
$cckey = get_theme_mod( 'key' ); | |
// fill in these values | |
$firstName = ""; | |
$lastName = ""; | |
$emailAddr = "[email protected]"; | |
$zip = ""; | |
// represents the contact list identification number(s) | |
$contactListId = 1; | |
$contactListId = (!is_array($contactListId)) ? array($contactListId) : $contactListId; | |
$post = new SimpleXMLElement('<entry></entry>'); | |
$post->addAttribute('xmlns', 'http://www.w3.org/2005/Atom'); | |
$title = $post->addChild('title', ""); | |
$title->addAttribute('type', 'text'); | |
$post->addChild('updated', date('c')); | |
$post->addChild('author', ""); | |
$post->addChild('id', 'data:,none'); | |
$summary = $post->addChild('summary', 'Contact'); | |
$summary->addAttribute('type', 'text'); | |
$content = $post->addChild('content'); | |
$content->addAttribute('type', 'application/vnd.ctct+xml'); | |
$contact = $content->addChild('Contact'); | |
$contact->addAttribute('xmlns', 'http://ws.constantcontact.com/ns/1.0/'); | |
$contact->addChild('EmailAddress', $emailAddr); | |
$contact->addChild('FirstName', $firstName); | |
$contact->addChild('LastName', $lastName); | |
$contact->addChild('PostalCode', $zip); | |
$contact->addChild('OptInSource', 'ACTION_BY_CUSTOMER'); | |
$contactlists = $contact->addChild('ContactLists'); | |
// loop through each of the defined contact lists | |
foreach($contactListId AS $listId) { | |
$contactlist = $contactlists->addChild('ContactList'); | |
$contactlist->addAttribute('id', 'http://api.constantcontact.com/ws/customers/' . $ccuser . '/lists/' . $listId); | |
} | |
$posturl = "https://api.constantcontact.com/ws/customers/{$ccuser}/contacts"; | |
$authstr = $cckey . '%' . $ccuser . ':' . $ccpass; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $posturl); | |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |
curl_setopt($ch, CURLOPT_USERPWD, $authstr); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post->asXML()); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/atom+xml")); | |
curl_setopt($ch, CURLOPT_HEADER, false); // Do not return headers | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // If you set this to 0, it will take you to a page with the http response | |
$result = curl_exec($ch); | |
curl_close($ch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment