Adding contacts to a campaign

SOLVED
Highlighted
Advisor

Adding contacts to a campaign

I am using PHP through the PHP wrapper to create and send an email that will provide people with a link to their daily biblIe reading they have signed up for.

 

I am basically taking the example code for creating and scheduling a campaign and adapting it for my purposes. I am not sure how to associate a contact list to the campaign, I have created a test contact list with one email in it.

 

I am able to create a campaign, but when I go to schedule it, it says. I've attached the code as well.

 

Thanks in advance for your assistance.

 

Array
(
    [0] => Array
        (
            [error_key] => http.status.bad_request
            [error_message] => No contacts to send.
        )

)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Advisor

Re: Adding contacts to a campaign

Elijah,

 

I love the name by the way!

 

Thanks for the code.

 

Initially I got an error message saying it needed to be a string. Once I put quotes around the numeric id it worked.

 

The time for the scheduling is not what I expected so I'll have to check into that. The time I wanted was and hour post submission it wound up being 3 o'clock this afternoon some how!

 

Thanks for you assistance!!

View solution in original post

9 REPLIES 9
Highlighted
Moderator

Re: Adding contacts to a campaign

If you look at the Campaign object (https://github.com/constantcontact/php-sdk/blob/development/src/Ctct/Components/EmailMarketing/Campa... you'll notice there is an array of lists as one of the attributes.  If you get the lists in your account, you simply need to add one or more lists to this array before creating or updating the email campaign.  You can see more about ContactLists here: https://github.com/constantcontact/php-sdk/blob/development/src/Ctct/Components/Contacts/ContactList...

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Advisor

Re: Adding contacts to a campaign

Dave,

 

I got page not found in Git-Hub when I clicked on the links. Could you check them please. I noticed in the original code then retrieved a list to present to the user to select from. Being I am not using a form. I tried to create an array with the name of the contact list using a number of "token names" from either the code or documentation I could find, but was getting an error messages like the one below. 

 

Thanks for your assistance. You guys must have the patience of a saint!

 

Error Creating Campaign

Array
(
    [0] => Array
        (
            [error_key] => json.field.invalid
            [error_message] => #/ContactLists: Property was found but is not permitted at this location.
        )

)
Highlighted
Moderator

Re: Adding contacts to a campaign

Sorry, using Chrome to post responses today due to security issues with IE.  It doesn't do well with the editor and copy/pasting content in this blog.  Basically, added my punctuation as part of the links.  Just updated them now, should work fine.

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Advisor

Re: Adding contacts to a campaign

echo "<br>Variables finished being set here<br><br>";

function createCampaign($name, $subject, $from_name, $from_email,
            $greeting_string, $reply_to, $text_content,
            $html_content, $email_format)
{
    $cc = new ConstantContact(APIKEY);
    $campaign = new Campaign();
    $campaign->name = $name;
    $campaign->subject = "$subject";
    $campaign->from_name = $from_name;
    $campaign->from_email = $from_email;
    $campaign->greeting_string = $greeting_string;
    $campaign->reply_to_email = $reply_to;
    $campaign->text_content = $text_content;
    $campaign->email_content = $html_content;
    $campaign->email_content_format = $email_format;
    $campaign->sent_to_contact_lists = array(1369672605);
    return $cc->addEmailCampaign(ACCESS_TOKEN, $campaign);
}

 

I can't figure out wha't wrong with the above code.

I've tried it with quotes and without

 

I keep getting this error message.

 

Error Creating Campaign

Array
(
    [0] => Array
        (
            [error_key] => json.type.invalid
            [error_message] => #/sent_to_contact_lists/0: Value is of a disallowed type. Allowed types are: Null, Object.
        )

)
Highlighted
Honored Contributor

Re: Adding contacts to a campaign

With the code you've provided, the only issue is that the PHP wrapper code expects the array of lists to be ContactList objects rather than just a numeric list ID. You can correct this by updating the line where you set the list like this: 

// Create the contact list array
$contact_lists = array();
// Add a list to the array
$contact_lists[] = ContactList::create(array("id" => 1369672605));
// Set the campaign to use the array of ContactList objects.
$campaign->sent_to_contact_lists = $contact_lists;

 

If you are still running into issues after this, please reply back with some information and we will be glad to continue assisting you!

Elijah G.
API Support Engineer
Highlighted
Advisor

Re: Adding contacts to a campaign

Elijah,

 

I love the name by the way!

 

Thanks for the code.

 

Initially I got an error message saying it needed to be a string. Once I put quotes around the numeric id it worked.

 

The time for the scheduling is not what I expected so I'll have to check into that. The time I wanted was and hour post submission it wound up being 3 o'clock this afternoon some how!

 

Thanks for you assistance!!

View solution in original post

Highlighted
Honored Contributor

Re: Adding contacts to a campaign

I am glad that I was able to assist you!

 

In regards to the scheduling time problem, I believe that what you are running into is a time zones confusion. With the ISO-8601 standard, the default timezone is UTC/GMT unless specified otherwise and it appears to be set to use UTC in your code when creating the time string to submit via the API. You will want to make sure that your calculated time is properly converted to UTC before you convert it to a string and submit it to the API in order to ensure that the email is scheduled for the proper time.

Elijah G.
API Support Engineer
Highlighted
Occasional Visitor

Re: Adding contacts to a campaign

Hi, 

 

I am currently struggling with a similar error. Where did you add this piece of code in the php-sdk?

 

Currenly the only thing I have added to my code is my API key and Access Key. 

 

When I add the API Key and Acess Key, and run the addOrUpdateContact.php I get the following error:

 

Array
(
    [0] => Array
        (
            [error_key] => json.type.invalid
            [error_message] => #/lists/0/id: Value is of a disallowed type. Allowed types are: String.
        )

)

 

Any ideas how to address this error? I have attached my code, if you look in addOrUpdateContact.php, I have added my keys, that is the only thing I have changed in it. 

 

Would really appreciate your help.

 

Best,

Mohamad

 

 

 

 

Highlighted
Honored Contributor

Re: Adding contacts to a campaign

Hello Mohamad,

 

After reviewing this, I was not able to replicate the error. However, based on your description, it seems that your environment is forcing the list ID to be read as an integer instead of a string. You can correct this by forcing it to be a string. Here are two changes that will resolve this problem in the example:

 

Line 54:

 

Before:
            $contact->addList($_POST['list']);
After
            $contact->addList($_POST['list']."");

 

Line 72:

Before:
            $contact->addList($_POST['list']);
After
            $contact->addList($_POST['list']."");

 

 

Another thing that is worth checking is to ensure that you are using PHP  5.3+.

 

If you experience any further issues, please let us know!

 

Best Regards,

Elijah G.
API Support Engineer
Developer Portal

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

Constant Contact would like to hear from you

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured