The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

Problem copying a campaign

Highlighted
Occasional Contributor

Problem copying a campaign

Hello

 

My client has created several email models on his account. I need to use this models to create new emails that must be sent in certain cases. I thought of doing it like this:

 

$campaigns = $ConstantContact->getCampaigns();
if(count($campaigns))
  foreach($campaigns['campaigns'] as $key=>$campaign)
  {
      if($campaign->name=='Email model name')
       {   
           $modelCampaign = $ConstantContact->getCampaignDetails($campaign);
           print_r($modelCampaign);

           $myCampaign = new Campaign();
           $myCampaign->name = "Email name based on date ".date('Y-m-d');
           $myCampaign->subject = $modelCampaign ->subject;
           $myCampaign->fromName = $modelCampaign ->fromName;
           $myCampaign->greetingName = $modelCampaign ->greetingName;
           $myCampaign->greetingString = $modelCampaign ->greetingString;
           $myCampaign->emailContentFormat = $modelCampaign ->emailContentFormat;
           $myCampaign->emailContent = $modelCampaign ->emailContent;
           $myCampaign->textVersionContent = $modelCampaign ->textVersionContent ;
           $VerifiedAddresses = $ConstantContact->getVerifiedAddresses();
           $newCampaign = $ConstantContact->addCampaign($myCampaign, $VerifiedAddresses[addresses][0]);
           print_r($newCampaign);
           break;
      }
  }

 

The problem is the new campaign is not created because the $modelCampaign somehow doesn't have all the details. The error i get is:

Constant Contact Campaign Error: 'textVersionContent' was required but not supplied
Constant Contact HTTP Request Exception: Error 400: Error: Line 1: cvc-pattern-valid: Value '' is not facet-valid with respect to pattern '((F|f)(I|i)(R|r)(S|s)(T|t)(N|n)(A|a)(M|m)(E|e))|((L|l)(A|a)(S|s)(T|t)(N|n)(A|a)(M|m)(E|e))|((F|f)(I|i)(R|r)(S|s)(T|t)(A|a)(N|n)(D|d)(L|l)(A|a)(S|s)(T|t)(N|n)(A|a)(M|m)(E|e))|((N|n)(O|o)(N|n)(E|e))' for type 'greetingchoices'.

Error: Line 1: cvc-type.3.1.3: The value '' of element 'GreetingName' is not valid.
Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in my_path/ctct/Collections.php on line 412

Warning: simplexml_load_string(): Error 400: Error: Line 1: cvc-pattern-valid: Value '' is not facet-valid with re in my_path/ctct/Collections.php on line 412 Warning: simplexml_load_string(): ^ in my_path/ctct/Collections.php on line 412

Fatal error: Call to a member function Attributes() on a non-object in my_path/ctct/Components.php on line 547

 

Can you please give me any advice?

Thank you

6 REPLIES 6
Highlighted
Employee

Re: Problem copying a campaign

What if you try to set:

 

$myCampaign = new Campaign();
$myCampaign = $modelCampaign;

 Since the object is that of the same type, I believe that it should make an exact copy as it is of the same type.

Ryan Davis
Quality Engineer
Highlighted
Occasional Contributor

Re: Problem copying a campaign

Unfortunately that leads to the same error; as if the object returned by the getCampaignDetails method doesn't have all the fields required to create a new campaign. And i don't know another way to select a campaign and copy it...

Highlighted
Employee

Re: Problem copying a campaign

Try doing something like this,

 

<?php
include 'ConstantContact.php';

$ConstantContact = new ConstantContact("basic", "apikey", "username", "password");

$myVerifiedAddress = $ConstantContact->getVerifiedAddresses();
$fromEmail = $myVerifiedAddress[addresses][0];
$replyEmail = $myVerifiedAddress[addresses][0];
$myCampaigns = $ConstantContact->getCampaigns(); // get all campaigns


do { 
	 // do while so we can see if there are multiple pages.  
	 // This process can take a long time as it handles many requests
	$x = 0; // Start X over for every page of campaigns	
	foreach($myCampaigns[campaigns] as $Campaign[$x])
	{
		echo $Campaign[$x]->id;
		if($Campaign[$x]->id == "http://api.constantcontact.com/ws/customers/{username}/campaigns/{CampaignID}")
		{
			$thisCampaignDetails = $ConstantContact->getCampaignDetails($Campaign[$x]);
			$newCampaign = new Campaign();
			$newCampaign = $thisCampaignDetails;
			$newCampaign->name = rand() . $thisCampaignDetails->name;
		}

		$x++;
	}
}while ($myCampaigns[nextlink] != false);

$ConstantContact->addCampaign($newCampaign, $fromEmail);

?>

 

Ryan Davis
Quality Engineer
Highlighted
Occasional Contributor

Re: Problem copying a campaign

Hi Ryan,

 

The above code works well for copying the email campaign.

But is there any specific reason behing getting the details of all the campaigns. The process of getting all the campaigns and comparing if its id matches the one we are trying to copy.

 

Is there any way of just get the details of single campaign, change the name and add it as a new campaign.

I am getting an error if i try do this way.

 

Please suggest.

 

Thanks,

Harika

 

Highlighted
Employee

Re: Problem copying a campaign

Hey,

 

Yes you want to get the campaign details so you can have a FULL campaign object of the data that you wish to copy.  Upon doing a GET To campaigns, it only brings in a summary of data which would not be enough to create your new campaign.  The ID however is returned in the summary. 

 

I am writing some changes to the wrapper which may make this simpler in the future by adding a new feature:

 

GetCampaignByID which will return the campaign  details with the campain ID supplied.  This should be released sometime this week.

 

Let me know if this helps.

Ryan Davis
Quality Engineer
Highlighted
Occasional Contributor

Re: Problem copying a campaign

 

Thanks for the reply Ryan.

This answers my question. Please let us know once getCampaignByID feature is ready.

 

Thanks,

Harika

Developer Portal

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

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured