cancel
Showing results for 
Search instead for 
Did you mean: 

adding a new contact

Occasional Contributor

adding a new contact

i'm using the php library to add a new contact to a list..here's my code:

 

<?php
require_once 'src/Ctct/autoload.php';


use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\Contacts\EmailAddress;
use Ctct\Exceptions\CtctException;

 

//my credentials
    $username = '';
    $apiKey = '';
    $consumerSecret = '';
    $apipass = '';       
   
    //authenticate
    $ConstantContact = new ConstantContact("basic", $apiKey, $username, $apipass);

    $Contact = new Contact();
    $Contact->emailAddress = "test@gmail.com";
    $Contact->firstName = "ftest";
    $Contact->lastName = "ltest";
    $Contact->lists = array('http://api.constantcontact.com/ws/customers/$username/lists/1');

    $NewContact = $ConstantContact->addContact($Contact);
    
    if($NewContact)
    {
        mail('mymail@gmail.com','contact addeded','success');

    }
    else
    {
        mail('mymail@gmail.com','contact not addeded','failure');
    }
?>       

 

but when i run the above code, i get the error:

 

Catchable fatal error: Argument 2 passed to Ctct\ConstantContact::addContact() must be an instance of Ctct\Components\Contacts\Contact, none given, called in /home/masterss/public_html/projects/constantcontactapi/contact.php on line 28 and defined in /home/masterss/public_html/projects/constantcontactapi/src/Ctct/ConstantContact.php on line 153

 


seems to be a problem when  addContact() is called..why i'm i getting the above error?..any assistance would be much appreciated

1 REPLY 1
Moderator

Re: adding a new contact

Hi Georgette,

 

It looks like you are mixing an old and new PHP library in the code you provided. 

 

This is the PHP SDK method to use to add or update a contact. 

 

The v2 API does not accept the basic authentication method. In order to get an OAuth 2.0 access token (which is required for the v2 API) follow the instructions here.

 

Regards, 

 

Rich Marcucella
Sr. Technical Writer, Content Developer
Web Services Team
http://developer.constantcontact.com