0

Here's how I do it:

$debug = true;
$auth = base64_encode( 'user:'. APIKEY );

$data = array(
    'schedule_time' => date('Y-m-d H:i:s',strtotime("+1 hour")),
    'timewarp' => false
    );
$json_data = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.SERVER.'.api.mailchimp.com/3.0/campaigns/'.$campaign_id.'/actions/schedule');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
    'Authorization: Basic '. $auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

$result = curl_exec($ch);

if ($debug) {
    var_dump($result);
}

This would give an error:

schedule_time may only be in 15 minute intervals, e.g. 13:15 not 13:10

loQ
  • 2,147
  • 18
  • 21

1 Answers1

0

This is old but might as well answer for anyone else that stumbles upon this:

So as the error message says you can only schedule a campaign to go out on a multiple of 15 minutes such as X:00, X:15, X:30, and X:45. Therefore your use of

date('Y-m-d H:i:s',strtotime("+1 hour"))

Means your scheduling will always fail unless you get some really good timing.

What I found useful is to give the user a Timepicker dropdown with a step of 15:

$('.timepicker').timepicker({ 'scrollDefault': 'now', 'step': 15, 'timeFormat': 'H:i' });

As well as a typical date picker, when the scheduling is triggered I then combine the two into the ATOM format that Mailchimp wants:

    var date = $("#schedule_date").val(); //Date from the Datepicker     
    var time = $("#schedule_time").val(); //Time from the 15min step Timepicker   
    var offset = new Date().getTimezoneOffset()/60; // I believe Mailchimp likes to have it in UTC    
    var datetime=new Date(date+'T'+time+':00'+((offset<0)?"+":"-")+((offset>-10 && offset<10)?"0":"")+offset+':00');  // Constructing ATOM time (example: 2005-08-15T15:52:01+00:00)    
    var datestring = datetime.toISOString();   
    datestring = datestring.substring(0, datestring.length - 5)+'+00:00';