Create a New Campaign

<?php

define('IBF_BASE_URL', 'http://if.inboxfirst.com/ga/api');
define('IBF_API_KEY', 'MTo1MD1zZeUyMyIxMTRjNGExzTdlZDQ0NmI1ZmVmYWIwOWY3nmI3ZjIy');
$apikey=base64_decode(IBF_API_KEY);

function InboxFirst_studio_create_campaign($params) {
  // Gather parameters needed to communicate with IF.
  $listID   = $params['mailing_list_id'];
  $url      = IBF_BASE_URL.'/v2/mailing_lists/'.$listID.'/campaigns';
  $ch       = curl_init($url);
  $campaign = array('campaign' => $params);
  $json     = json_encode($campaign);

  // Set up cURL to communicate this message.
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_USERPWD, $apikey);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($json),
  ));

  // Execute the command, retrieve HTTP errors.
  $response_raw = curl_exec($ch);
  $error_test   = curl_error($ch);
  $err          = curl_errno($ch);

  // Don't leave the connection hanging.
  curl_close($ch);

  // First, check if there was an HTTP error.
  if ($err != 0) {
    $rv = "ERROR: cURL - $err $error_test\n";
    return $rv;
  }

  // Decode InboxFirst's response JSON.
  $result = json_decode($response_raw);

  // Return an appropriate response.
  if (isset($result->success)) {
    if ($result->success == false) {
      $return_value = "Error:";

      if (isset($result->error_message)) {
        $return_value .= " " . $result->error_message;
      }
    } else if ($result->success == true) {
      $return_value = "OK";
    } else {
      $return_value = "Error: unknown status";
    }
  } else {
    $return_value = "Error: unknown response from IF Station";
  }

  return $return_value;
}

$params = array(
  'name'                           => 'test api campaign 4',
  'mailing_list_id'                => 1,
  'segmentation_criteria_id'       => null,
  'contents' => array(
    array(
      'name' => 'My A Content',
      'format'  => 'html',
      'subject' => 'my subject',
      'html'    => 'here is my html content',
      'text'    => '',
    ),
    array(
      'name' => 'My B Content',
      'format'  => 'html',
      'subject' => 'bbb my subject',
      'html'    => 'bbb here is my html content',
      'text'    => '',
    ),
  ),
);

$result = InboxFirst_studio_create_campaign($params);
echo $result . "\n";
?>