Skip to content

Instantly share code, notes, and snippets.

@phpguru
Last active August 29, 2015 14:27
Show Gist options
  • Save phpguru/8ae0ea2e299d984c56e8 to your computer and use it in GitHub Desktop.
Save phpguru/8ae0ea2e299d984c56e8 to your computer and use it in GitHub Desktop.
Sample code showing SalesForce ExactTarget FuelSDK-PHP Retrieve Subscribers with Filters
<?php
date_default_timezone_set('America/Phoenix');
ini_set('include_path', ini_get('include_path').':/home/app/vendor/fuelsdk-php');
try {
$outputfile = __DIR__.'/subscribers2014b.csv';
if (file_exists($outputfile)) {
unlink($outputfile);
}
require('ET_Client.php');
$client = new ET_Client();
$list = new ET_Subscriber();
$list->authStub = $client;
$list->props = array('ID', 'EmailAddress', 'Status', 'CreatedDate', 'UnsubscribedDate');
$twentyThirteenFilter = array(
'LeftOperand' => array(
'Property' => 'CreatedDate',
'SimpleOperator' => 'lessThan',
'DateValue' => '2014-01-01T00:00:00.000000'
),
'LogicalOperator' => 'AND',
'RightOperand' => array(
'Property' => 'CreatedDate',
'SimpleOperator' => 'greaterThan',
'DateValue' => '2012-12-31T23:59:59.999999'
)
);
$twentyFourteenFilter = array(
'LeftOperand' => array(
'Property' => 'CreatedDate',
'SimpleOperator' => 'lessThan',
'DateValue' => '2015-01-01T00:00:00.000000'
),
'LogicalOperator' => 'AND',
'RightOperand' => array(
'Property' => 'CreatedDate',
'SimpleOperator' => 'greaterThan',
'DateValue' => '2014-07-15T16:26:00.000001'
)
);
$twentyFifteenFilter = array(
'Property' => 'CreatedDate',
'SimpleOperator' => 'greaterThan',
'DateValue' => '2014-12-31T23:59:59.999999'
);
$list->filter = $twentyFourteenFilter;
$response = $list->get();
$iterations = 0;
capture($response, $outputfile, $iterations);
while ($response->moreResults == 1) {
$iterations++;
$response = new ET_Continue($client, $response->request_id);
capture($response, $outputfile, $iterations);
}
echo PHP_EOL.PHP_EOL.PHP_EOL.PHP_EOL.PHP_EOL."DONE after ".$iterations." iterations.".PHP_EOL;
} catch (Exception $e) {
echo $e->getMessage();
}
function capture($response, $outputfile, $iterations) {
$iterations++;
echo PHP_EOL.PHP_EOL."Iteration # ".$iterations.PHP_EOL;
$i=0;
if ( ! is_array($response->results) ) {
echo "Error: No results from last call...".PHP_EOL;
print_r($response);
return;
}
foreach($response->results as $obj) {
$line = '"'.
$obj->ID .'","'.
$obj->EmailAddress .'","'.
$obj->Status .'","'.
$obj->CreatedDate .'","';
if (property_exists($obj, 'UnsubscribedDate')) {
$line .= $obj->UnsubscribedDate .'"';
} else {
$line .= '"';
}
$line .= PHP_EOL;
file_put_contents($outputfile, $line, FILE_APPEND);
echo ++$i % 1000 === 0 ? PHP_EOL.$line : '.';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment