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.

Need Help With API Integration

Highlighted
Occasional Participant

Need Help With API Integration

Hi,

 

I'm working on integrating some Constant Contact functionality into a site I am working on. On my home page I'm trying to add a form field to join a mailing list. I am using the API because I'm trying to do this as an ajax submission. I have the ajax javascript part working, but the PHP is returning a number of errors. Here's what it is returning:

 

Notice: Trying to get property of non-object in /www/eh16214/public_html/assets/bin/cc_class.php on line 266 Warning: Invalid argument supplied for foreach() in /www/eh16214/public_html/assets/bin/cc_class.php on line 266 Notice: Trying to get property of non-object in /www/eh16214/public_html/assets/bin/cc_class.php on line 283 Warning: Invalid argument supplied for foreach() in /www/eh16214/public_html/assets/bin/cc_class.php on line 283

 

I must confess I am in a bit over my head on the php part of this, but I'm trying to modify the PHP sample code to get the functionality I'm looking for. If I'm understanding the php code correctly, I am taking the post variable with the email address and I'm checking to see if it is in the contact database already. If it is I return one string and if it isn't I return another. Eventually I plan to set it to redirect to an add form or an update form. When I submit an email address I'm currently getting the string of error messages above along with the text string it is supposed to return if the address is not in the database. When I log in and check the contact list, the email address is not being added, which I assume is because of the php errors.

 

Ideally, I would like this to be as simple as possible. In fact, I originally wanted to just drop the email address into a mailing list via an ajax submission, without worrying about other info. I'm kind of at a loss as to how to proceed so if you can render any assistance I would greatly appreciate it.

 

Thanks,

 

Brian Rivet

6 REPLIES 6
Highlighted
Moderator

Re: Need Help With API Integration

PHP warnings indicate that the content passed to a foreach loop is not what it was execting.  Since they are warnings, they are actually really just informative.  It usually indicates that something passed to the foreach was not the type expected or not an array.  As long as you are getting a 201 Created response back and not something in the 4xx range, you are still creating the Contact.  You can suppress warnings by configuring PHP to not show warnings on your production server or logging them to a file for viewing later.

 

Hope this helps.

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Participant

Re: Need Help With API Integration

Hi Dave,

 

Thanks for getting back to me. Unfortunately I don't think I'm going to be able to shut off the php errors. I'm using Expression Engine as my CMS on my site and I think it may affect my control panel functionality to change the php error setting. I'm not sure though. I was hoping that you might be able to help me find the causes of the notices so I could correct them and not trip the warnings. Also, I rand a search after submitting the email address and the email address I entered is not coming up in any searches of my contact lists, so I don't believe it is actually being added anywhere. If you can give me any pointers as to how to proceed I'd appreciate it. I can post the php code I'm using if that would help.

 

Thanks,

 

Brian

Highlighted
Moderator

Re: Need Help With API Integration

I think I know the line of code you mentioned but if you could put the coe you've created that actually uses the library, that would help quite a bit. 

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Participant

Re: Need Help With API Integration

Hi Dave,

 

Thanks for your help on this. Here's the php code that my form will send the email address to:

 

<?php
include_once('cc_class.php');
$ccContactOBJ = new CC_Contact();
//if we have post fields means that the form have been submitted.
if (!empty($_POST)) {
    $contact = $ccContactOBJ->getSubscribers(urlencode($_POST['src_mail']));
    if (empty($contact['items']) || empty($_POST['src_mail'])) {
        $message = 'You have been added to our database.';
        echo $message;       
    }
    else {
        $message = 'You are already listed in our database';
        echo $message;       
    }
}
?>

 

I made a small change to the cc_class.php file, here are the lines I changed:

 

var $login = '****'; //Username for your account
        var $password = '*****'; //Password for your account
        var $apikey = '*****'; // API Key for your account.
       
        // CONTACT LIST OPTIONS
        var $contact_lists = array('Master List - No Duplicates'); // Define which lists will be available for sign-up.
        var $force_lists = true; // Set this to true to take away the ability for users to select and de-select lists
        var $show_contact_lists = false; // Set this to false to hide the list name(s) on the sign-up form.
        // NOTE - Contact Lists will only be hidden if force_lists is set to true. This is to prevent available checkboxes form being hidden.

 

 

my plan is it possibly replace the "You have been added" messages with redirects to a couple of forms where the user can add more contact info, but at the very least I'd like the script to add the email address to the master email list if it isn't there already.

 

Please let me know if you have any recommendations as to the best way to proceed with this.

 

Thanks,

 

Brian

 

Highlighted
Moderator

Re: Need Help With API Integration

I edited your post to remove your username/password/APIKey for security reasons.  It looks like the problem is that the simplexml_load_string() method on line 264 is not correctly parsing the XML and returning an XML object into $parsedReturn. 

 

Going to need to run this code on a local debug environment here to try to reproduce what is being returned in the $return var the line before and what's being returned back.  Because that is not returning a parsed XML object, the foreach() loops are not able to work correctly and thus you aren't able to get the correct information back.

 

While I'm not a PHP developer myself, we do have a PHP expert on staff that will be in later today and will be able to get a more detailed answer for you.  If you can post the value of $return on line 264 for me, I may be able to figure out the problem just from the data point of view.  Also, would be interested to know what the value is that you're passing on for the $_POST['src_mail'] option.

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Participant

Re: Need Help With API Integration

Hi Dave,

 

Don't worry about elevating this up the support chain. I spoke to my client earlier and they want to go a simpler route with it, so it looks like I'm not going to be using the API on this after all. I appreciate your help thus far though.

 

Thanks!

 

Brian

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