phpfui / ConstantContact

MIT License
16 stars 7 forks source link

Examples on how to add a contact #7

Closed degive closed 2 years ago

degive commented 2 years ago

Do you have example code on how to add a contact by any chance?

I see that you have to create a PHPFUI\ConstantContact\Definition\ContactPostRequest first and then do a \PHPFUI\ConstantContact\V3\Contacts.

Your help is very much appreciated.

Thank you.

Michel

phpfui commented 2 years ago

I have something like this, but take a look at the Constant Contact API docs, not sure if $contact has other fields, as I did this a while ago:

$email_address = new \PHPFUI\ConstantContact\Definition\EmailAddressPut($contact['email_address']);
$email_address->permission_to_send = 'explicit';
$contact['email_address'] = $email_address;
$contactBody = new \PHPFUI\ConstantContact\Definition\ContactPutRequest($contact);
$contactBody->update_source = 'Account';
// $member contains the normal stuff, see below
$contactBody->street_addresses = [new \PHPFUI\ConstantContact\Definition\StreetAddressPut([
    'kind' => 'home',
    'street' => $member['address'],
    'city' => $member['town'],
    'state' => $member['state'],
    'postal_code' => $member['zip'],
    'country' => 'USA', ])];

$contactClient->put($contact['contact_id'], $contactBody);

if (! $contactClient->success())
    {
    WriteDebug($contactClient->getStatusCode(), $contactClient->getLastError());
    }
degive commented 2 years ago

Couldn't get this to work, so instead I decided to use the SignUpForm

Here is the code, the response returned is null, not sure why, maybe you do. Appreciate your help!

    $client = new \PHPFUI\ConstantContact\Client($apiKey, $secret, $url);
    ...

$params['email_address'] = $email;
$params['first_name']=$first;
$params['last_name']=$last;
$contact=new PHPFUI\ConstantContact\Definition\ContactCreateOrUpdateInput($params);
echo "Contact:<br>";print_r($contact);echo "<br>";

$signup = new \PHPFUI\ConstantContact\V3\Contacts\SignUpForm($client);
$response=$signup->post($contact);
echo "Signup:<br>";print_r($response);echo "<br>";

Thank you.

degive commented 2 years ago

I actually got this to work by adding the list_memberships parameter.

    $uuid= new PHPFUI\ConstantContact\UUID($list);
    $params['list_memberships']=$uuid;
phpfui commented 2 years ago

Not sure. I would look at the Constant Contact API for examples of how the SignUpForm end point works, then see if the PHP code is sending the same thing to the endpoint. You can track it in the Client class. The library is a wrapper, it does not really validate the logic. It may be that you need another condition satisfied for it to work correctly.

phpfui commented 2 years ago

Ah, messages crossed in the ether. I was correct, another dependency was needed!

Not sure what error messages are coming back from Constant Contact, if any. I think the library returns them correctly, but if not, let me know.