Skip to content

Instantly share code, notes, and snippets.

@thesjg
Last active August 5, 2016 15:31
Show Gist options
  • Save thesjg/faa9080f42119a62ac98907c41e64293 to your computer and use it in GitHub Desktop.
Save thesjg/faa9080f42119a62ac98907c41e64293 to your computer and use it in GitHub Desktop.
<?php
$mc_apikey = 'xxx';
$request_url = 'https://us13.api.mailchimp.com/3.0/lists/ccea54209d/members/?offset=%s&count=%s';
$offset = 0;
$count = 200;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: user ' . $mc_apikey));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$mcount = 1;
while (1) {
$t_url = sprintf($request_url, $offset, $count);
curl_setopt($ch, CURLOPT_URL, $t_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = json_decode(curl_exec($ch));
foreach ($result->members as $member) {
if (empty($member->merge_fields->RDOB) && empty($member->merge_fields->RDOB2)) {
continue;
}
$diff1 = 367;
$diff2 = 367;
if (!empty($member->merge_fields->RDOB)) {
$now = new DateTime();
$dob = new DateTime($member->merge_fields->RDOB);
$birthday = new DateTime();
$birthday->setDate($now->format('Y'), $dob->format('m'), $dob->format('d'));
$diff1 = $now->diff($birthday)->format("%a");
$age1 = $now->diff($dob)->y + 1;
}
if (!empty($member->merge_fields->RDOB2)) {
$now = new DateTime();
$dob = new DateTime($member->merge_fields->RDOB2);
$birthday = new DateTime();
$birthday->setDate($now->format('Y'), $dob->format('m'), $dob->format('d'));
$diff2 = $now->diff($birthday)->format("%a");
$age2 = $now->diff($dob)->y + 1;
}
$diff = min($diff1, $diff2);
if ($diff == $diff1)
$age = $age1;
else
$age = $age2;
// echo "MIGHT update $member->email_address to ACTION bday$diff\n";
if ($diff == 367)
continue;
$data = array(
'email_address' => $member->email_address,
'merge_fields' => array(
'ACTION' => 'bday' . $diff . '-' . $age,
)
);
$chupd = curl_init();
curl_setopt($chupd, CURLOPT_URL, 'https://us13.api.mailchimp.com/3.0/lists/ccea54209d/members/' . md5($member->email_address));
curl_setopt($chupd, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: user ' . $mc_apikey));
curl_setopt($chupd, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($chupd, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chupd, CURLOPT_TIMEOUT, 10);
curl_setopt($chupd, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($chupd, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($chupd, CURLOPT_POSTFIELDS, json_encode($data));
curl_exec($chupd);
echo "$mcount Updating $member->email_address to ACTION bday" . $diff . "-" . $age . "\n";
$mcount++;
}
if (!is_object($result) || count($result->members) < 1 || $offset + $count > $result->total_items) {
break;
}
$offset += $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment