Create and Send Campaign Using MooSend API

<?php

$apikey = "<MOOSEND-API-KEY-HERE>";
$listID = "<MOOSEND-LIST-ID-HERE>";
$ep = "https://api.moosend.com/v3/";

$ch = curl_init();

$url = $ep."campaigns/create.json?Format=json&apikey=".$apikey;

$bulkPostFields = array (
  'Name' => '<CAMPAIGN-NAME-HERE>',
  'Subject' => '<CAMPAIGN-SUBJECT-HERE>',
  'SenderEmail' => '<SENDER-EMAIL-ID-HERE>',
  'ReplyToEmail' => '<SENDER-EMAIL-ID-HERE>',
  'ConfirmationToEmail' => '<SENDER-EMAIL-ID-HERE>',
  'MailingLists' => [
array (
'MailingListID' => "<MOOSEND-LIST-ID-HERE>",
'SegmentID' => "<MOOSEND-SEGMENT-ID-HERE>"
),
],
'WebLocation' => "PATH-TO-HTML-EMAILER",
);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($bulkPostFields));
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Accept: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}else{
$res = json_decode($result);
$campaignID = $res->Context;
}
curl_close ($ch);

//campaign send here
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $ep."campaigns/".$campaignID."/send.json?apikey=".$apikey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Accept: application/json";
$headers[] = "Content-Length: 0";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}else{
echo $result;
}
curl_close ($ch);

?> 

0 comments :