forked from mrmarkfrench/silverpop-php-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiTest.php
executable file
·210 lines (186 loc) · 6.17 KB
/
apiTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use SilverpopConnector\SilverpopConnector;
echo "Parsing credentials file...\n";
$credentials = parse_ini_file(__DIR__.'/authData.ini', true);
echo "Setting base URL...\n";
SilverpopConnector::getInstance($credentials['silverpop']['baseUrl']);
if (!empty($credentials['silverpop']['username'])) {
echo "Authenticating to XML API...\n";
SilverpopConnector::getInstance()->authenticateXml(
$credentials['silverpop']['username'],
$credentials['silverpop']['password']
);
} else {
echo "No XML credentials. Performing combined authentication against REST...\n";
}
echo "Authenticating to REST API...\n";
SilverpopConnector::getInstance()->authenticateRest(
$credentials['silverpop']['client_id'],
$credentials['silverpop']['client_secret'],
$credentials['silverpop']['refresh_token']
);
echo "Retrieving lists...\n";
$result = SilverpopConnector::getInstance()->getLists();
if (count($result)) {
echo " -- Found ".count($result)." lists.\n";
$listId = null;
$listMembers = PHP_INT_MAX;
foreach ($result as $list) {
if ($list->TYPE != 0) {
// Only want a database, not some other list type
continue;
}
if ($listMembers <= 1 || ($list->SIZE > 1 && $list->SIZE < $listMembers)) {
$listId = (string)$list->ID;
$listMembers = (string)$list->SIZE;
$listName = (string)$list->NAME;
}
}
if (empty($listId)) {
echo " -- No populated lists found!\n";
die("Exiting");
} else {
echo " -- Selected list \"{$listName}\" ({$listId}), with {$listMembers} members.\n";
}
} else {
echo " -- No lists found!\n";
die("Exiting");
}
echo "Setting date format to default MM/dd/yyyy\n";
SilverpopConnector::getInstance()->setDateFormat('MM/dd/yyyy');
echo "Retrieving list meta data...\n";
$result = SilverpopConnector::getInstance()->getListMetaData($listId);
$columns = array();
foreach ($result->COLUMNS->COLUMN as $column) {
$columns[] = (string)$column->NAME;
}
echo ' -- Found '.count($columns)." columns.\n";
echo "Exporting recipient data...\n";
$result = SilverpopConnector::getInstance()->rawRecipientDataExport();
$jobId = $result['jobId'];
$filePath = $result['filePath'];
echo " -- Job Id: {$jobId}, file path: {$filePath} ";
//900k csv/90k zip took 1min
//SK 20140203 time page load, part 1 of 2
$time = microtime(); $time = explode(' ', $time); $time = $time[1] + $time[0];
$pageLoadStart = $time;
echo getJobStatusLoop($jobId);
//SK 20140203 time page load, part 2 of 2
$time = microtime(); $time = explode(' ', $time); $time = $time[1] + $time[0];
$pageLoadFinish = $time;
$pageLoadTotal = round(($pageLoadFinish - $pageLoadStart), 4);
echo 'Loop completed in '.$pageLoadTotal.' seconds on '.date('Y-m-d\TH:i:s.000P');
//See also: https://github.com/Boardroom/smart-popup/blob/master/test_sp_export_api.php
function getJobStatusLoop($jobId, $numAttempts = 600) {
$isCompleted = false;
$attempts = 0;
$response = null;
//sleep(300); // Sleep for the first five minutes no matter what.
while (!$isCompleted && $attempts < $numAttempts) {
$jobStatus = SilverpopConnector::getInstance()->getJobStatus($jobId);
switch ($jobStatus) {
case "COMPLETE":
$isCompleted = true;
break;
case "RUNNING":
case "WAITING":
// Give the job time to complete.
sleep(60);
break;
case "CANCELED": //yes Silverpop spelled this wrong.
case "ERROR":
default:
$response = "\nError: Silverpop get job status execution failed. Attempts: {$attempts} ";
exit(-1);
break;
}
// Increment the attempts; limit attempts.
$attempts++;
}
if ($isCompleted === false) {
//$isCompleted = $response;
$response .= "\nNot completed. {$attempts} attempts. ";
} else {
$response .= "\nJob successfully completed. {$attempts} attempts. ";
}
//return $isCompleted;
return $response;
}
/*
echo "Exporting list {$listId}...\n";
$result = SilverpopConnector::getInstance()->exportList(
$listId,
0,
time(),
'ALL',
'CSV',
$columns);
$jobId = $result['jobId'];
$filePath = $result['filePath'];
$filePath = str_replace('/download/', '', $filePath);
echo "Waiting for export job to complete...\n";
$done = false;
while ($done == false) {
$result = SilverpopConnector::getInstance()->getJobStatus($jobId);
echo " -- Job {$jobId} status is {$result}";
switch ($result) {
case 'COMPLETE':
echo "\n";
$done = true;
break;
case 'ERROR':
echo "\n";
exit;
default:
for ($i=0; $i<15; $i++) {
echo '.';
sleep(1);
}
echo ".\n";
}
}
echo "Streaming exported file...\n";
$result = SilverpopConnector::getInstance()->streamExportFile($filePath, null);
echo " -- Parsing stream data...\n";
$csvLines = explode("\n", trim($result));
$header = str_getcsv(array_shift($csvLines));
$data = array();
foreach ($csvLines as $line) {
$lineData = str_getcsv($line);
$lineData = array_map('trim', $lineData);
$data[] = array_combine($header, $lineData);
}
echo " -- Parsed ".count($data)." contacts from stream.\n";
echo "Selecting one contact to examine...\n";
$contact = $data[0];
echo " -- Contact email {$contact['Email']} (Name: {$contact['FirstName']} {$contact['LastName']}) selected.\n";
echo "Creating a new contact...\n";
$newContact = array(
'Email' => '[email protected]',
'FirstName' => 'TestGuy',
'LastName' => 'ExampleFellow',
);
$recipientId = SilverpopConnector::getInstance()->addRecipient($listId, $newContact);
echo " -- New contact created with ID {$recipientId}\n";
echo "Updating test contact...\n";
$updatedContact = $newContact;
$updatedContact['FirstName'] = "UpdatedTestValue";
SilverpopConnector::getInstance()->updateRecipient($listId, $recipientId, $updatedContact);
//echo "Adding Universal Behavior event to test contact...\n";
//$attributes = array(
// 'Email' => $email,
// 'Tweet Id' => '24',
// 'Author Id' => '34',
// 'Retweeted Id' => '44',
// 'First Name' => 'Richard',
// 'Last Name' => 'Riddick',
// 'BrandTag' => 'Silverpop',
// 'Entities' => '#amplify',
// );
//SilverpopConnector::getInstance()->createEvent(7, date('Y-m-d\TH:i:s.000P'), $attributes);
echo "Deleting test contact...\n";
$email = $newContact['Email'];
SilverpopConnector::getInstance()->removeRecipient($listId, $email, array('Email'=>$email));
*/