Hi
I am trying to write the contacts to local db so that I can do all the management from there and push whatever changes I need to do from our own app. I have this script that grabs the emails and lists but it limited to 50. I am not sure how paging would be added to this in order to grab the 2 large lists (5000, 2000 each)
See code below: (The code works, I just need to get all the contacts)
Cheers
Gary
<?php
require_once('db_connect.php');
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
// Require the autoloader
require_once 'sdk/src/Ctct/autoload.php';
use Ctct\ConstantContact;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\EmailMarketing\Campaign;
use Ctct\Components\EmailMarketing\MessageFooter;
use Ctct\Components\EmailMarketing\Schedule;
use Ctct\Exceptions\CtctException;
// Enter your Constant Contact APIKEY and ACCESS_TOKEN
define("APIKEY", "xxxxxxxxxxxxxxxxxxxx");
define("ACCESS_TOKEN", "xxxxxxxxxxxxxxxxxxxx");
$cc = new ConstantContact(APIKEY);
$date = date('Y-m-d\TH:i:s\.000\Z', strtotime("+1 month"));
function getContacts($listID,array $params)
{
$cc1 = new ConstantContact(APIKEY);
return $cc1->getContactsFromList(ACCESS_TOKEN,$listID);
}
// Attempt to get the lists in this account
try {
$lists = $cc->getLists(ACCESS_TOKEN);
} catch (CtctException $ex) {
die();
}
// Start loopin through the lists
foreach ($lists as $list) {
$listID = $list->id;
//
// Attempt to get the emails from the list
try {
$contacts = getContacts($listID,array('limit' => 10000)); // Tried to change the array number
} catch (CtctException $ex) {
die();
}
// Loop through the contacts and add them to the db
foreach ($contacts->results as $contact) {
$insertSQL = sprintf("INSERT INTO emails (email, emailid, list) VALUES (%s, %s, %s)",
GetSQLValueString($contact->email_addresses[0]->email_address, "text"),
GetSQLValueString($contact->id, "int"),
GetSQLValueString($listID, "int"));
mysql_select_db($database_dbconnect, $dbconnect);
$Result1 = mysql_query($insertSQL, $dbconnect) or die(mysql_error());
// Echos for visual confirmation, comment out for live script
echo $contact->email_addresses[0]->email_address;
echo "<br/>";
} // End looping through the contacts
// Echos for visual confirmation, comment out for live script
echo "finished list ". $listID;
echo "<br/>";
} // End looping thrlough the lists
?>
Hello Gary,
I just wanted to let you know that we did receive your email on the same subject to our webservices@ email address and I've sent you the modifications that were needed there to get this working fully along with some additional information.
Please let us know if you have any additional issues!
Sincerely,
Elijah G.
API Support Specialist
I came here from search results, looking for the same answer. I didn't find it so I had to learn the hard way. For the benefit of others, here are two sample codes that paginate query results.
Thank you for sharing this with the community! I definitely believe that people will be helped by this. In addition, we are currently working on some improvements for our API documentation which will include greater volume of samples for situations like this.
If you do have any questions about the APIs, please feel free to make a post on these forums and we will be happy to help!
Sincerely,
The holidays have come and gone. For many seasonal businesses, this means the rush of shoppers has decreased as well. Instead of turning off the lights and waiting for spring, make your email marketi...
See Article