Any simple php example?

ErnestG8
Regular Participant

Any simple php example?

I am wondering if there is any simple example how to add new contact to a list using php. I found in your documentation sample code for php, but this is not a very simple example, the package has 1000+ files and is more than 5Mb. I can not include this into our extension that are sold to our customers. Is that necessary for just simple example how to add new contact, is there any simplier example?

 

Tnx!

3 REPLIES 3
ErnestG8
Regular Participant

I managed to create sample code myself. This is code for checking and adding new contact. 47 lines of code, much simplier than 1000+ files and 30000+ lines of code:)

 

<?php

$apiKey = "YOUR_API_KEY";
$token = "YOUR_TOKEN";

$listId = "YOUR_LIST_ID";
$email = "test@test.si";
$url = "https://api.constantcontact.com/v2/contacts?email=$email&status=ALL&limit=50&api_key=$apiKey";

$header[] = "Authorization: Bearer $token";
$header[] = 'Content-Type: application/json';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch));
curl_close($ch);
if(curl_error($ch)) echo 'error:' . curl_error($ch);


if(!$response->results){
$url = "https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_VISITOR&api_key=$apiKey";
$body = '{
"lists": [
{
"id": "'.$listId.'"
}
],
"confirmed": false,
"email_addresses": [
{
"email_address": "'.$email.'"
}
],
"first_name": "Ronald",
"last_name": "Martone"
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
if(curl_error($ch)) echo 'error:' . curl_error($ch);
var_dump($response);
}

LarryV375
Frequent Participant

I like your example. You helped me out alot.

HannahB540
Occasional Participant

Very nice. Seems to be the only working example in the whole internet. Good job.

Developer Portal

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

Visit Page