Any simple php example?

Highlighted
Occasional Contributor

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!

2 REPLIES 2
Highlighted
Occasional Contributor

Re: Any simple php example?

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);
}

Highlighted
Occasional Contributor

Re: Any simple php example?

I like your example. You helped me out alot.

Developer Portal

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

Visit Page

Register for our "Making Sense of Online Marketing for Retail" Webinar

Join us on Thursday, August 13th at 2pm ET for a free webinar where we'll discuss how to incorporate online marketing into your retail store.

Read More
Featured