henning glatter-götz

ExactTarget SOAP Upsert with PHP

There is quite a bit of documentation for the ExactTarget SOAP API that includes lots of code samples. But unfortunately the PHP code samples are not quite as plentiful as the .NET and Java ones. After lots of searching and lot of trial and error I finally got my upsert working. An upsert is a SOAP method in the ExactTarget SOAP API that will either update or insert a record depending on whether it is already present or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$uo = new ExactTarget_UpdateOptions();
$uo->SaveOptions = array();

$so = new ExactTarget_SaveOption();
$so->PropertyName = 'DataExtensionObject';
$so->SaveAction = ExactTarget_SaveAction::UpdateAdd;

$uo->SaveOptions[] = $so;
$uoSo = ETCore::toSoapVar($uo, 'UpdateOptions');

$request = new ExactTarget_UpdateRequest();
$request->Options = $uoSo;
$request->Objects = array($deoSo);
$result = $soapClient->Update($request);

Where $deoSo is a DataExtension Object that has been converted to a SoapVar and $soapClient is an ExactTarget soap client instance. The way you get the upsert behavior is to set the saveaction to ExactTarget_SaveAction::UpdateAdd.

If you are looking for a minimal wrapper library that includes the above method take a look at this library on gitub. This library gleans quite a few things from the Doctrine ORM, so if you are familiar with that you should feel right at home.

Comments