PHP add campaign

Regular Participant

PHP add campaign



I am using the php wrapper class provided by CC.  I need to add a campaign from my application using this code, but I can't seem to find any documentation on how to do so.  The example scripts included don't seem to cover this operation.


Is there any docs for this framework, or a sample of code to add campaigns with?


Hey Lauren,


The documentation for creating a campaign can be found here. The sample code on that page gives a basic example of the XML layout and tags needed for the campaign.


Let us know if you have any questions about it, and we'll be here to help!

Nick Galbraith
Support Engineer



In order to use the PHP wrapper and create a campaign you need to do the following:

// include the wrapper
include ('wrapper/ConstantContact.php');

// Instantiate your ConstantContact object
// Pass the appropriate credentials

$ConstantContact = ConstantContact("Basic", "api-key", "username", "password");

// Get all lists
$Lists = $ConstantContact->getLists();

// Get verified email addresses
$VerifiedEmailAddresses = $ConstantContact->getVerifiedAddresses();

// Create a new Campaign Object

 * Create the Campaign

// Istantiate $myCampaign to the Campaign
$myCampaign = new Campaign();

// Set the internal name of Campaign / Random number appended so it wont duplicate
$myCampaign->name = "Test Campaign " . rand(1, 2000);

// Set the lists that this email will go to
$myCampaign->lists = array($Lists['lists'][0]->id, $Lists['lists'][1]->id);

$myCampaign->subject = "This is the subject";
$myCampaign->emailContentFormat = "HTML";

// HTML must be entered with out entities encoded.  The wrapper will dot his for you
$myCampaign->emailContent = "<HTML><BODY>This is your test email</BODY></HTML>";
$myCampaign->textVersionContent = "Test";

$AddCampaign = $ConstantContact->addCampaign($myCampaign, $VerifiedEmailAddresses['addresses'][0]);

Please let us know if that helps. 

Ryan Davis
Quality Engineer
Regular Participant

Thanks guys... I was able to get a campaign added via the API with the above help.


I am now having an issue attempting to schedule a campaign to send.  I have been given the proper auth to do so by CC, and have received the schedule campaign API doc.  Currently I am trying to schedule with this code:


$xmlMessage = "<?xml version='1.0' encoding='UTF-8'?>";
    $xmlMessage = $xmlMessage . '<entry xmlns="">';
    $xmlMessage = $xmlMessage . '<link href="/ws/customers/<username>/campaigns/'.$campaignID.'/schedules/1" rel="edit" />';
    $xmlMessage = $xmlMessage . '<id><username>/campaigns/'.$campaignID.'/schedules/1</id>';
    $xmlMessage = $xmlMessage . '<title type="text">2010-11-13T15:03:35Z</title>';
    $xmlMessage = $xmlMessage . '<updated>2010-11-13T20:03:35.000Z</updated>';
    $xmlMessage = $xmlMessage . '<author> <name>Constant Contact</name> </author>';
    $xmlMessage = $xmlMessage . '<content type="application/vnd.ctct+xml">';
    $xmlMessage = $xmlMessage . '<Schedule xmlns="" id="'.$campaignID.'/schedules/1">';
    $xmlMessage = $xmlMessage . '<ScheduledTime>'.$dtStart.'T15:10:00.000Z</ScheduledTime>';
    $xmlMessage = $xmlMessage . '</Schedule> </content> </entry>';
    $URL = "<username>/campaigns/<campaignID>/schedules";

    $ch = curl_init($URL);
    curl_setopt($ch, CURLOPT_MUTE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, "$xmlMessage");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);



But I get back the following:


HTTP Status 401 - An Authentication object was not found in the SecurityContext

Where am I going wrong?


Hi Lauren,


I was able to get your code to work, but I had to change this


curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));

to this:


curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml', 'Content-Length: ' . strlen($xmlMessage)));


I also added these fields:

curl_setopt($ch, CURLOPT_USERPWD, 'My API key%my user name:my password');
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");


I also replaced the items in the $URL variable that are currently enclosed in angle brackets with the appropriate information. Also ensure that your API key and password are entered correctly and that the URL is correct.



Richard Maxwell
Support Engineer, Constant Contact
Regular Participant



I updated my code to match what you have above, but it's still not working properly.  I get the following returned from the CURL call:


Error 404: Not Found


I have double checked that I have all my API key / username / password info entered correctly.  What could be going wrong?




Regular Participant

Anyone able to offer any additional help with this issue?

The 404 error would indicate the URL you are making the POST request does not exist.  Are you making the POST request to an actual existing campaign in your account?

Dave Berard
Senior Product Manager, Constant Contact
Occasional Participant

Hi, I applied the same code. I can get list and contacts as well. and in reply of this code $AddCampaign = $ConstantContact->addCampaign($myCampaign, $VerifiedEmailAddresses['addresses'][0]); I didnt get any error or response but after that I tied to look my account's Email -> My Emails Section but here I cant find any new created campaign. Am I looking on correct place? If yes than why it is not creating? Please help me. And one more thing I also tried with curl with{user-name}/campaigns url it is asking for username and password I use my account credential but it is still not alowing so, which credentials do I have to use? Thanks For Help.



Sorry you're having trouble adding an email through the API.  I'm concerned that you're not getting any response back from our servers--a success, OK, or error message.  The URI you are using is correct.  In the body of the request, are you using XML formatted like on this page, which shows how to format XML for an email?


The username it could be looking for is APIKey%username, where you replace the api key and username with your own, and input your account password normally.


If you continue to get no response whatsoever after trying to post an email, I would recommend using RESTClient to troubleshoot, since it can be used to make calls to our servers simply.  This is a link on how to use RESTClient.


I hope that helps!


Best Regards,

Shannon W.

API Support Engineer

Regular Participant

For some reason, the following statement does not work (does not return).


include ('wrapper/ConstantContact.php');


Does any one have any suggestion on how to fix it?



Regular Participant

My web-site is hosted by YAHOO.

My PHP version is 4.3.11.

This is very old PHP.


What ConstantContact wrapper will work with my PHP version 4.3.11?


Where I can down load such ConstantContact wrapper?



there are three ZIP files.

Which one will work with PHP version 4.3.11?




The library you're trying to use requires PHP 5.1.3 (See our README here).  For PHP 4.3, the required curl options and PHP features do not exist to support this library.  A version for PHP 4.X is available through one of our partners, but there is a fee associated with it.  You can find a 4.X version here:

Dave Berard
Senior Product Manager, Constant Contact

For those arriving at this topic in search of an example on creating campaigns via our PHP Wrapper Library, I've added an example to the examples folder in our PHP Wrapper Library that consists of a simple form for creating an email campaign.

Mark Coleman
Support Engineer
Developer Portal

View API documentation, code samples, get your API key.

Visit Page