Skip to content

Instantly share code, notes, and snippets.

@rotten77
Created March 9, 2016 06:25
Show Gist options
  • Save rotten77/7708570fa1e90e50ee6c to your computer and use it in GitHub Desktop.
Save rotten77/7708570fa1e90e50ee6c to your computer and use it in GitHub Desktop.
Examples of usage of Campaign Monitor API
<?php
/**
*=====================================================================
* Campaign Monitor API
* PHP library: http://campaignmonitor.github.io/createsend-php/
*/
define('API_KEY', '');
define('LIST_ID', '');
/**
*=====================================================================
* Update options of custom field
*/
include_once dirname(__FILE__) . "/createsend-php/csrest_lists.php";
$wrap = new CS_REST_Lists(LIST_ID, API_KEY);
$options = array(
"Value 1",
"Value 2",
"Value 3",
"Value 4",
);
// Keep existing?
$optionsKeepExisting = false;
$result = $wrap->update_field_options('[Options]', $options, $optionsKeepExisting);
// Result
if($result->was_successful()) {
echo 'OK';
var_dump($result);
} else {
echo 'ERROR '.$result->http_status_code;
var_dump($result->response);
}
/**
*=====================================================================
* Send contact
*/
include dirname(__FILE__) . '/createsend-php/csrest_subscribers.php';
$wrap = new CS_REST_Subscribers(LIST_ID, API_KEY);
$contact = array();
$contact['EmailAddress'] = 'john.doe@gmail.com';
$contact['Name'] = 'John Doe';
$contact['CustomFields'] = array();
$contact['CustomFields'][] = array('Key' => 'CustomField1', 'Value' => 'Custom Value of Field 1');
$userOptions = array(
"Value 2",
"Value 5",
);
foreach($userOptions as $optionValue) {
$contact['CustomFields'][] = array('Key' => 'Options', 'Value' => $optionValue);
}
// check if user is in database yet
$result = $wrap->get($contact['EmailAddress']);
if(!$result->was_successful()) {
// add new
$result = $wrap->add($contact);
} else {
// ...or update
$result = $wrap->update($contact['EmailAddress'], $contact);
}
// Result
if($result->was_successful()) {
echo 'OK';
var_dump($result);
} else {
echo 'ERROR '.$result->http_status_code;
var_dump($result->response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment