Skip to content

EtClient Simple Usage

Jeremy Zhang edited this page Oct 19, 2016 · 1 revision

EtClient

Abstract

The EtClient is the main conduit to the ExactTarget API. All interactions with the ExactTarget servers go through it. It can serve as the mediator or a factory class. Other classes can be used outside the context of the EtClient file.

When using the EtClient as a factory, the EtClient class comes with a few magic functions which allow you to request a specific action and class, which each execute SOAP calls to preform their intended action.

Purpose

The bread and butter of this library. The ExactTarget starterKit provides a series of samples leans to a very buck-shot scattered method for use of SOAP calls. This puts all interactions with the SOAP calls in one place.

Functions

There are three (3) function calls built into this client:

  • buildTriggeredSend
  • sendEmail
  • getDefinitionOfObject

buildTriggeredSend

The **buildTriggeredSend **function accepts two (2) arguments the first -- required -- is the triggeredSendKey which is the "External Key" for the specific Triggered Email and can be found in the Web UI under Interactions > Messages > TriggeredSends.

The second (optional) is an array of options built as a key-value pair which should correspond with available variables on the EtTriggeredSendDefinition class.

$EtClient->buildTriggeredSend($triggeredSendKey, [$optionalAttributesArray]);

sendEmail

The **sendEmail **function accepts two (2) required variables the first is an Et-object which is valid for sending (e.g. EtTriggeredSend, EtEmail, etc) the second is the SendType the type of the object which should be used for SOAP communications.

$EtClient->sendEmail($EmailableClass, $EmailableClassType);

getDefinitionOfObject

The **getDefinitionOfObject **function is a function provided by the ExactTarget PHP Starter Kit used for getting the properties of an ExactTarget record (think of MySQL's DESCRIBE) and returning the properties that are "Retrievable".

$EtClient->getDefinitionOfObject($EtClass);

Magic

There are four (4) magic function calls built into this client

  • create - Creates and returns a specified Et object
  • recall - Retrieve a populated Et-class for an existing ExactTarget record based on filter parameters provided (returns false if none exists)
  • update - Updates an existing ExactTarget record based on the Et object given
  • bundle - Batch update method which will call an Update request used to add many subscribers, etc.

Examples

Mediator/ Straight OO

Example (subscriber):

// instantiates the Client for SOAP Communications
$client = new EtClient($userName,$password);

// Subscriber object is passed the client 
$subscriber    = new EtSubscriber($client);
$subscriberKey = $emailAddress = "[email protected]";

// Find a subscriber from ExactTarget by subscriber key (email address is optional)
$subscriber->find($subscriberKey, $emailAddress);

// Add Subscriber to a specific list (by list id)
$subscriber->addToList("24601");

// Build a new Attribute 
$newAttrib = new EtAttribute();
$newAttrib->setName('Metroid Save Code'); // Attribute Name
$newAttrib->setValue("------ ---mE3 l-y000 00y00j"); // Attribute Value

// setAttributes is be used for initially setting attributes on subscriber
$subscriber->setAttributes(array($newAttrib)); 

// updateAttribute is used for objects which already have attributes
// $subscriber->updateAttribute($newAttrib); 

// Update Subscriber record
$subscriber->save(); 

/* either by the save method on EtSubscriber or
 * via the EtClient 
 * $client->updateSubscriber($subscriber, 'updateOnly');   
 */

Example (Triggered Send):

// Initialize EtClient
$client = new EtClient($userName,$password); 

// create Triggered Send object giving it access to the EtClient object
$ts = new EtTriggeredSend($client);

// Define which triggered send to use
$ts->useKey("defeatedMotherBrain");

// create Subscriber object giving it access to the EtClient object
$sub = new EtSubscriber($client);

// search for a user by subscriber key [required] and 
// email address [optional]
$sub->find("[email protected]","[email protected]");

// define the subscribers the Triggered Send is bound for
$ts->setSubscribers(array($sub));

// execute send
$ts->send();

Factory

Example (create subscriber):

// instantiates the Client for SOAP Communications
$client = new EtClient($userName,$password);

$userData   = array(
     'SubscriberKey'       => "[email protected]",
     'EmailAddress'        => "[email protected]",
     'EmailTypePreference' => EtEmailType::Text,
);
$subscriber = $client->createSubscriber($userData);

// Add Subscriber to a specific list (by list id)
$subscriber->addToList("24601");

// Build a new Attribute 
$newAttrib = new EtAttribute();
$newAttrib->setName('Metroid Save Code'); // Attribute Name
$newAttrib->setValue("------ ---mE3 l-y000 00y00j"); // Attribute Value

// setAttributes is be used for initially setting attributes on subscriber
$subscriber->setAttributes(array($newAttrib)); 

// updateAttribute is used for objects which already have attributes
// $subscriber->updateAttribute($newAttrib); 

$client->updateSubscriber($subscriber, 'updateOnly');   

Example (recall subscriber):

// instantiates the Client for SOAP Communications
$client = new EtClient($userName,$password);

 $subscriberInfo = array(
  array(
      "Name" => "SubscriberKey",
      "Value" => "[email protected]",
      "operator" => "equals",
      ),
  array(
      "Name" => "EmailAddress",
      "Value" => "[email protected]",
      "operator" => "equals",
      ),
  );
$subscriber = $client->recallSubscriber($subscriberInfo);

Example (get subscriber attribute via email):

// [email protected] is email adress of subscriber
$client              = new EtClient($userName,$password);
$subscriber =  new EtSubscriber($client);
$subscriber->find('[email protected]');
$attributes = $subscriber->Attributes;
var_dump($attributes);die(); // display all data;
//foreach ($attributes as $attribute) {
//    ${$attribute->Name} = $attribute->Value;
//}   

Example (update subscriber attribute):

$client     = new EtClient($userName,$password);
$subscriber =  new EtSubscriber($client);
$subscriber->find('[email protected]');

// FirstName is one of profile attribute of subscriber,
// Example_NAME should be subscriber First Name you want update
$Attribute_FirstName   = new EtAttribute('FirstName','Example_NAME');

$subscriber->updateAttribute($Attribute_Times);
$subscriber->save('UPDATEONLY');

Example (get all subscribers by list id):

// there are multiple functionality to get all subscribers
$subscriberFilter = new EtSimpleFilterPart();
$subscriberFilter->Property = "ListID";
$subscriberFilter->SimpleOperator = EtSimpleOperators::EQUALS;
$subscriberFilter->Value = array('Your_Own_listID');

$statusFilter = new EtSimpleFilterPart();
$statusFilter->Property = "Status";
$statusFilter->SimpleOperator = EtSimpleOperators::EQUALS;
$statusFilter->Value = array('Active'); // we target active subscriber

$filter = new EtComplexFilterPart();
$filter->LeftOperand = $subscriberFilter;
$filter->LogicalOperator = EtLogicalOperators::LOGICAL_AND;
$filter->RightOperand = $statusFilter;

$client      = new EtClient($userName,$password);
$subscribers = $client->recallListSubscriber($filter);

Example (get the all emails we send to an subscriber):

$email = '[email protected]';
$filterPart = new EtSimpleFilterPart();
$filterPart->Property = 'SubscriberKey';
$filterPart->Value = array($email);
$filterPart->SimpleOperator = EtSimpleOperators::EQUALS;
$HistoryRecorders = $client->simpleQuery($type,['key'=>'SubscriberKey','value'=>$email]);
if (!empty($HistoryRecorders)) { 
    var_dump($HistoryRecorders);die();
    $SendIDs = [];
//    foreach ($HistoryRecorders as $HistoryRecorder) {
//        $SendIDs[] = $HistoryRecorder->SendID;
 //   }
}