function AddSubscriberToList($emailAddress, $listIDs, $subscriberKey = null) { $newSub = new ET_Subscriber(); $newSub->authStub = $this; $lists = array(); foreach ($listIDs as $key => $value) { $lists[] = array("ID" => $value); } //if (is_string($emailAddress)) { $newSub->props = array("EmailAddress" => $emailAddress, "Lists" => $lists); if ($subscriberKey != null) { $newSub->props['SubscriberKey'] = $subscriberKey; } /*} else if (is_array($emailAddress)) { $newSub->props = array(); for ($i = 0; $i < count($emailAddress); $i++) { $copyLists = array(); foreach ($lists as $k => $v) { $NewProps = array(); foreach($v as $prop => $value) { $NewProps[$prop] = $value; } $copyLists[$k] = $NewProps; } $p = array("EmailAddress" => $emailAddress[$i], "Lists" => $copyLists); if (is_array($subscriberKey) && $subscriberKey[$i] != null) { $p['SubscriberKey'] = $subscriberKey[$i]; } $newSub->props[] = $p; } }*/ // Try to add the subscriber $postResponse = $newSub->post(); if ($postResponse->status == false) { // If the subscriber already exists in the account then we need to do an update. // Update Subscriber On List if ($postResponse->results[0]->ErrorCode == "12014") { $patchResponse = $newSub->patch(); return $patchResponse; } } return $postResponse; }
$subCreate->props = array("EmailAddress" => $SubscriberTestEmail, "Lists" => array("ID" => $newListID)); $postResult = $subCreate->post(); print_r('Post Status: ' . ($postResult->status ? 'true' : 'false') . "\n"); print 'Code: ' . $postResult->code . "\n"; print 'Message: ' . $postResult->message . "\n"; print 'Results Length: ' . count($postResult->results) . "\n"; print 'Results: ' . "\n"; print_r($postResult->results); print "\n---------------\n"; if (!$postResult->status) { // If the subscriber already exists in the account then we need to do an update. // Update Subscriber On List if ($postResult->results[0]->ErrorCode == "12014") { // Update Subscriber to add to List print "Update Subscriber to add to List \n"; $subPatch = new ET_Subscriber(); $subPatch->authStub = $myclient; $subPatch->props = array("EmailAddress" => $SubscriberTestEmail, "Lists" => array("ID" => $newListID)); $patchResult = $subPatch->patch(); print_r('Patch Status: ' . ($patchResult->status ? 'true' : 'false') . "\n"); print 'Code: ' . $patchResult->code . "\n"; print 'Message: ' . $patchResult->message . "\n"; print 'Results Length: ' . count($patchResult->results) . "\n"; print 'Results: ' . "\n"; print_r($patchResult->results); print "\n---------------\n"; } } // Retrieve all Subscribers on the List print "Retrieve all Subscribers on the List \n"; $getList = new ET_List_Subscriber();
$postContent->authStub = $myclient; $postContent->props = array("ListName" => "Test List for Message Guide Send", "Description" => "This list was created with the PHPSDK", "Type" => "Private"); $postResponse = $postContent->post(); print_r('Post Status: ' . ($postResponse->status ? 'true' : 'false') . "\n"); print 'Code: ' . $postResponse->code . "\n"; print 'Message: ' . $postResponse->message . "\n"; print 'Results Length: ' . count($postResponse->results) . "\n"; print 'Results: ' . "\n"; print_r($postResponse->results); print "\n---------------\n"; if ($postResponse->status) { $listID = $postResponse->results[0]->NewID; $SubscriberTestEmail = "*****@*****.**"; // Create Subscriber on List print "Upsert Subscriber to List \n"; $subCreate = new ET_Subscriber(); $subCreate->authStub = $myclient; $subCreate->props = array("EmailAddress" => $SubscriberTestEmail, "Lists" => array("ID" => $listID)); $putResult = $subCreate->put(); print_r('Put Status: ' . ($putResult->status ? 'true' : 'false') . "\n"); print 'Code: ' . $putResult->code . "\n"; print 'Message: ' . $putResult->message . "\n"; print 'Results Length: ' . count($putResult->results) . "\n"; print 'Results: ' . "\n"; print_r($putResult->results); print "\n---------------\n"; if ($postResponse->status) { // Send the Message print "Send the new Message \n"; $sendMG = new ET_Message_Guide(); $sendMG->authStub = $myclient;
// Delete Subscriber print "Delete Subscriber \n"; $subDelete = new ET_Subscriber(); $subDelete->authStub = $myclient; $subDelete->props = array("EmailAddress" => $SubscriberTestEmail); $deleteResult = $subDelete->delete(); print_r('Delete Status: ' . ($deleteResult->status ? 'true' : 'false') . "\n"); print 'Code: ' . $deleteResult->code . "\n"; print 'Message: ' . $deleteResult->message . "\n"; print 'Results Length: ' . count($deleteResult->results) . "\n"; print 'Results: ' . "\n"; print_r($deleteResult->results); print "\n---------------\n"; // Retrieve Subscriber to confirm deletion print "Retrieve Subscriber to confirm deletion \n"; $retSub = new ET_Subscriber(); $retSub->authStub = $myclient; $retSub->filter = array('Property' => 'SubscriberKey', 'SimpleOperator' => 'equals', 'Value' => $SubscriberTestEmail); $retSub->props = array("SubscriberKey", "EmailAddress", "Status"); $getResult = $retSub->get(); print_r('Get Status: ' . ($getResult->status ? 'true' : 'false') . "\n"); print 'Code: ' . $getResult->code . "\n"; print 'Message: ' . $getResult->message . "\n"; print_r('More Results: ' . ($getResult->moreResults ? 'true' : 'false') . "\n"); print 'Results Length: ' . count($getResult->results) . "\n"; print 'Results: ' . "\n"; print_r($getResult->results); print "\n---------------\n"; // Do not run the "Retrieve All Subscribers" request for testing if you have more than 100,000 records in your account as it will take a long time to complete. /* // Retrieve All Subcribers with GetMoreResults
/** * @param $list Publication List ID * @param $email Email Address / SubscriberKey * @param $status Status /Unsubscribed-Active * @return bool|\ET_Patch */ public function UpdateListSubscriber($list, $email, $status) { $s = new \ET_Subscriber(); $s->authStub = $this->fuel; $s->props = array("SubscriberKey" => $email, "Lists" => array("ID" => $list), "Status" => $status); $getRes = $s->patch(); if ($getRes->status == true) { return $getRes; } else { Log::error('Error getting UpdateListSubscribers ET (UpdateListSubscribers)', [$getRes]); return false; } }