-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_et_data.php
459 lines (371 loc) · 15.4 KB
/
handle_et_data.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
header("Access-Control-Allow-Origin: *");
//echo '<pre>';
require('00-Includes/exacttarget_soap_client.php');
$wsdl = 'https://webservice.s4.exacttarget.com/etframework.wsdl';
$etusername = 'your_et_username';//Enter the username you use to login to Exact Target
$etpassword = 'your_et_password';//Enter the password you use to login to Exact Target
$subscriber = $_POST["Email_Address"];
$exkey = $_POST["Ex_Key"];
/*
Enter your field names below. They should match exactly in Exact Target and in your web form... and here, of course
*/
$fieldNames = array(
'Email Address',
'First Name',
'Last Name',
'United States Zip Code',
'State',
'Country',
'Birthday',
'Arts and Culture',
'Dining and Nightlife',
'Casinos',
'Shopping',
'Spas and Wellness',
'Sports',
'Golf',
'Tours and Sightseeing',
'Family Travel',
'Romantic Getaways',
'LGBT',
'Signup URL'
);
$mySub = array();
foreach($fieldNames as $field){
if(isset($_POST[str_replace(' ','_',$field)])){
$mySub[$field]=trim($_POST[str_replace(' ','_',$field)]);
}
}
function deleteDERow($subscriber,$client,$externalkey){
/*% Create ExactTarget_DataExtensionObject*/
$de = new ExactTarget_DataExtensionObject();
$de->CustomerKey = $externalkey; //key for the data extension we are updating
/*% ExactTarget_APIProperty */
$val1key = new ExactTarget_APIProperty();
$val1key->Name = "Email Address"; // name of DE field
$val1key->Value = $subscriber; // value for DE field
$de->Keys[] = $val1key;
$object = new SoapVar($de, SOAP_ENC_OBJECT, 'DataExtensionObject', "http://exacttarget.com/wsdl/partnerAPI");
/* Perform the delete */
$request = new ExactTarget_DeleteRequest();
$request->Options = NULL;
$request->Objects = array($object);
$results = $client->Delete($request);
$statuscode = $results->Results->StatusCode;
//var_dump($results);
echo "<br><br><br><br>";
if($statuscode == 'OK'){
echo "Successfully deleted DE row";
}else{
echo "There was an error deleting the row";
}
}
function findSubscriber($subscriber,$client){
/* Create the Retrieve request */
$request = new ExactTarget_RetrieveRequest();
$request->ObjectType= "DataExtensionObject[Consumer Master List]"; // replace DataExtensionName with the name of the data extension you are retrieving from
// define the data extension fields for the retrieve
$request->Properties[] = "Email Address"; // data extension field
$request->Properties[] = "Last Name";
// $request->Properties[] = "FIRST_NAME";
// $request->Properties[] = "SITE_GROUP";
//$request->Properties[] = "item_no"; // data extension field
// Setup a simple filter based on the key column you want to match on
$sfp= new ExactTarget_SimpleFilterPart();
$sfp->Value = array($subscriber);
$sfp->SimpleOperator = ExactTarget_SimpleOperators::equals;
$sfp->Property="Email Address";
$request->Filter = new SoapVar($sfp, SOAP_ENC_OBJECT, 'SimpleFilterPart', "http://exacttarget.com/wsdl/partnerAPI");
$request->Options = NULL;
$rrm = new ExactTarget_RetrieveRequestMsg();
$rrm->RetrieveRequest = $request;
$results = $client->Retrieve($rrm);
$checkit = $results->Results->Properties->Property[0]->Value;
if($checkit != "" ){
$rr = new ExactTarget_RetrieveRequest();
$rr->ObjectType = "Subscriber";
$rr->Properties = array();
$rr->Properties[] = "ID";
$rr->Properties[] = "SubscriberKey";
$rr->Properties[] = "EmailAddress";
$rr->Properties[] = "Status";
$sfp= new ExactTarget_SimpleFilterPart();
$sfp->Value = array($subscriber);
$sfp->SimpleOperator = ExactTarget_SimpleOperators::equals;
$sfp->Property="EmailAddress";
$rr->Filter = new SoapVar($sfp, SOAP_ENC_OBJECT, 'SimpleFilterPart', "http://exacttarget.com/wsdl/partnerAPI");
$rr->Options = NULL;
$rrm = new ExactTarget_RetrieveRequestMsg();
$rrm->RetrieveRequest = $rr;
$results = $client->Retrieve($rrm);
echo "Subscriber ".$subscriber." was found in the data extension";
echo "\n";
echo "This user is ".$results->Results->Status;
//var_dump($results);
//echo $results->OverallStatus;
}else{
echo "Could not find subscriber";
}
}
function addDERow($subscriber,$client,$externalkey){
/*% ExactTarget_DataExtensionObject */
$de = new ExactTarget_DataExtensionObject();
$de->CustomerKey = $externalkey; //external key/unique identifier for the data extension
/*% ExactTarget_APIProperty */
$val1key = new ExactTarget_APIProperty();
$val1key->Name = "Email Address"; // name of DE field
$val1key->Value = $subscriber; // value for DE field
// add field values to the data extension
$de->Properties[] = $val1key;
//$object = new SoapVar($de, SOAP_ENC_OBJECT, 'DataExtensionObject', "http://exacttarget.com/wsdl/partnerAPI");
$object = new SoapVar($de, SOAP_ENC_OBJECT, 'DataExtensionObject', "http://exacttarget.com/wsdl/partnerAPI");
// create the row of the data extension
$request = new ExactTarget_CreateRequest();
$request->Options = NULL;
$request->Objects = array($object);
$results = $client->Create($request);
$statuscode = $results->Results->StatusCode;
echo "<pre>";
var_dump($results);
echo "</pre>";
/*
//Ok documenting my code here. Stupid Exact Target doesn't have a consistent object structure
so I have to first check if an object property exists 'KeyErrors' and if it does, set the errMsg to it, if not, set it to another
part of the object */
if($statuscode == 'OK'){
$Msg = "Successfully added Data Extension Row";
}else{
if(property_exists($results->Results,'KeyErrors')){
echo $results->Results->KeyErrors->KeyError->ErrorMessage;
}else{
$Msg = $results->Results->ErrorMessage;
if (strpos($Msg,'duplicate key') !== false) {
echo "This email address is in the data extension";
}
}
}
//echo "<br><br>".gettype($keyErrs);//gets type of variable that $keyErrs is
}
function addUpdateDE($mySub,$client){
//validate email address and make sure they entered a zip code
if (!filter_var($mySub['Email Address'], FILTER_VALIDATE_EMAIL)) {
echo 'Please provide a valid email address.';return;
}
if(empty($mySub['United States Zip Code'])){
echo 'Zip code is required.';
mailStan("no name", $mySub['Email Address'], "null", "error", "no zipcode entered");
return;
}
if(empty($mySub['Birthday'])){
$mySub['Birthday'] = "01/01";
// echo $mySub['Birthday'];
}
if(preg_match("/O/", $mySub['Birthday'])){
$mySub['Birthday'] = preg_replace("/O/", "0", $mySub['Birthday']);
}
$ckdate = explode("/", $mySub['Birthday']);
/*
if($ckdate[0]>12 || $ckdate[0]<1){
echo 'Please make sure your birthdate is in a valid format MM/DD';
return;
}
if($ckdate[1]>31 || $ckdate[1]<1){
echo 'Please make sure your birthdate is in a valid format MM/DD';
return;
}
*/
if(checkdate($ckdate[0],$ckdate[1],2016)== false){
echo 'Please make sure your birthdate is in a valid format MM/DD';
return;
}
if(empty($mySub['First Name'])){
$mySub['First Name'] = "friends";
// echo $mySub['Birthday'];
}
/* ====================== END ERROR HANDLING SECTION ===================*/
$DE = new ExactTarget_DataExtensionObject();
$DE->CustomerKey="Cons-10001"; // unique identifier to the data extension
$keys = array_keys($mySub);//make a new array with keys from our fields array
/*Update can happen only if you have PrimaryKey column in the Data Extension*/
$apiPropertyKey = new ExactTarget_APIProperty();
$apiPropertyKey->Name=$keys[0]; // primary key of the data extension
$apiPropertyKey->Value=$mySub[$keys[0]]; // value of the primary key for the row we want to add/update
$DE->Keys[] = $apiPropertyKey; // add primary key field to the data exension
/*Update other fields*/
$i=0;
$apiProperty = array();
foreach($mySub as $key => $value){
$apiProperty[$i] =new ExactTarget_APIProperty();
$apiProperty[$i]->Name=$key; // field we want to add/update
$apiProperty[$i]->Value=$value; // new value for LastName
$i++;
}//loop end
$DE->Properties=$apiProperty;
$object1 = new SoapVar($DE, SOAP_ENC_OBJECT, 'DataExtensionObject', "http://exacttarget.com/wsdl/partnerAPI");
/*% Create the ExactTarget_SaveOption Object */
$saveOption = new ExactTarget_SaveOption();
//$saveOption->PropertyName=$DE;
$saveOption->PropertyName="DataExtensionObject";
$saveOption->SaveAction=ExactTarget_SaveAction::UpdateAdd; // set the SaveAction to add/update
// Apply options and object to request and perform update of data extension
$updateOptions = new ExactTarget_UpdateOptions();
$updateOptions->SaveOptions[] = new SoapVar($saveOption, SOAP_ENC_OBJECT, 'SaveOption', "http://exacttarget.com/wsdl/partnerAPI");
$request->Options = new SoapVar($updateOptions, SOAP_ENC_OBJECT, 'UpdateOptions', "http://exacttarget.com/wsdl/partnerAPI");
$request = new ExactTarget_CreateRequest();
$request->Options = $updateOptions;
$request->Objects = array($object1);
$results = $client->Update($request);
if($err){
}
$status = $results->OverallStatus;
if($status === "OK"){
//first sendmail so I can make sure everything went ok
//mailStan($mySub['First Name'], $mySub['Email Address'], $results, "success", $mySub['United States Zip Code']);
if ($mySub['Signup URL']=="http://www.sandiego.org/newsletter.aspx"){
echo "success-redirect";
}else if($mySub['Signup URL']=="mobile_site"){
echo '<p style="font-size:40px;padding:20px;">Thank you for signing up! We will be in touch soon! <a href="http://m.sandiego.org" >Click here</a> to go back to the site.</p>';
}
else{
echo "success-redirect";
}
}else{
$errMsg="Unknown error, please try again in a few minutes.";
mailStan($mySub['First Name'], $mySub['Email Address'], $results, "error", $mySub['United States Zip Code']);
echo $errMsg;
}
}
function varDumpToString($var) {
ob_start();
var_dump($var);
$result = ob_get_clean();
return $result;
}
function mailStan($name, $mail_from, $results, $status, $zip){
$mailToAddress = '[email protected]';
// TURN THIS ON AND MOVE TO BEFORE FIRST FUNCTION FOR TESTING
//$id = $_GET["id"];
//$e=$mySub['Email Address'];
//$z=$mySub['United States Zip Code'];
//$useragent=$_SERVER['HTTP_USER_AGENT'];
//$ip = $_SERVER['REMOTE_ADDR'];
// TURN THIS ON AND MOVE TO BEFORE FIRST FUNCTION FOR TESTING
// For human-readable results
//ob_start();
//var_dump($results);
//$result = ob_get_clean();
// ...or...
//$dataStr = print_r($data, TRUE);
if($results != "null"){
$resultText = varDumpToString($results);
}
//$message="Email Address: ".$e."Zip Code: ".$z." \n ".$useragent." \n ".$ip;
$message = "Zip Code: ".$zip." \n ";
$message .= "Email Address: ".$mail_from." \n";
$message .= $resultText;
if($status == "error"){
$subject = 'There has been a disturbance in the force.';
}else{
$subject = 'New newsletter signup.';
}
// From
$header="from: $name <$mail_from>";
//(to Email, Subject, Message, required headers)
$send_contact=mail('[email protected]',$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
//if($send_contact){
//echo "We've recived your contact information";
//}
//else {
//echo "ERROR";
//}
}
function updateListAttributes($mySub,$client){
$subscriber = new ExactTarget_Subscriber();
$subscriber->EmailAddress = $mySub['Email Address'];
$attribute1 = new ExactTarget_Attribute();
$attribute1->Name = 'Arts and Culture';
$attribute1->Value = $mySub['Arts and Culture'];
$attribute2 = new ExactTarget_Attribute();
$attribute2->Name = 'Dining and Nightlife';
$attribute2->Value = $mySub['Dining and Nightlife'];
$attribute3 = new ExactTarget_Attribute();
$attribute3->Name = 'Casinos';
$attribute3->Value = $mySub['Casinos'];
$attribute4 = new ExactTarget_Attribute();
$attribute4->Name = 'Shopping';
$attribute4->Value = $mySub['Shopping'];
$attribute5 = new ExactTarget_Attribute();
$attribute5->Name = 'Spas and Wellness';
$attribute5->Value = $mySub['Spas and Wellness'];
$attribute6 = new ExactTarget_Attribute();
$attribute6->Name = 'Sports';
$attribute6->Value = $mySub['Sports'];
$attribute7 = new ExactTarget_Attribute();
$attribute7->Name = 'Golf';
$attribute7->Value = $mySub['Golf'];
$attribute8 = new ExactTarget_Attribute();
$attribute8->Name = 'Tours and Sightseeing';
$attribute8->Value = $mySub['Tours and Sightseeing'];
$attribute9 = new ExactTarget_Attribute();
$attribute9->Name = 'Family Travel';
$attribute9->Value = $mySub['Family Travel'];
$attribute10 = new ExactTarget_Attribute();
$attribute10->Name = 'Romantic Getaways';
$attribute10->Value = $mySub['Romantic Getaways'];
$attribute11 = new ExactTarget_Attribute();
$attribute11->Name = 'LGBT';
$attribute11->Value = $mySub['LGBT'];
$subscriber->Attributes=array($attribute1,$attribute2,$attribute3,$attribute4,$attribute5,$attribute6,$attribute7,$attribute8,$attribute9,$attribute10,$attribute11);
// Specify the list to add the subscriber to
/*% ExactTarget_SubscriberList */
$sublist = new ExactTarget_SubscriberList();
$sublist->ID = 1298; // specify listID
$sublist->Action = "update"; // specify what action to apply to subscriber on list (delete, update are other options)
//use this if you want to also specify list id.
// $subscriber->Lists[] = new SoapVar($sublist, SOAP_ENC_OBJECT, 'SubscriberList', "http://exacttarget.com/wsdl/partnerAPI");
$object = new SoapVar($subscriber, SOAP_ENC_OBJECT, 'Subscriber', "http://exacttarget.com/wsdl/partnerAPI");
/*% ExactTarget_CreateRequest */
$request = new ExactTarget_CreateRequest();
// Configure Upsert Capabilities for the CreateRequest
/*% ExactTarget_CreateOptions */
$requestOptions = new ExactTarget_CreateOptions();
/*% ExactTarget_SaveOption */
$saveOption = new ExactTarget_SaveOption();
$saveOption->PropertyName = "*"; // * All props
$saveOption->SaveAction = "UpdateAdd"; // Specify upsert save action
$requestOptions->SaveOptions[] = new SoapVar($saveOption, SOAP_ENC_OBJECT, 'SaveOption', "http://exacttarget.com/wsdl/partnerAPI");
// Apply options and object to request
$request->Options = new SoapVar($requestOptions, SOAP_ENC_OBJECT, 'CreateOptions', "http://exacttarget.com/wsdl/partnerAPI");
$request->Objects = array($object);
// Execute the CreateRequest
$results = $client->Create($request);
//var_dump($results);
}
try//try to connect
{
$client = new ExactTargetSoapClient($wsdl, array('trace'=>1));
/* Set username and password here */
$client->username = $etusername;
$client->password = $etpassword;
}catch (SoapFault $e) {
/* output the resulting SoapFault upon an error */
echo "Error please contact webmaster";
var_dump($e);
}
//this code is just for our internal private form where we can check to see if people exist in the database and/or delete them.
if (isset($_POST["OptionButtons"])) {
$choice = $_POST["OptionButtons"];
switch($choice){
case 'remove': deleteDERow($subscriber,$client,$exkey); break;
case 'add': addDERow($subscriber,$client,$exkey);break;
case 'check':findSubscriber($subscriber,$client);break;
default: break;
}
}else{
updateListAttributes($mySub,$client);
addUpdateDE($mySub,$client);
}
?>