2016-08-29 15 views
1

我在Marketo上有一封電子郵件廣告系列使用PHP發送電子郵件。我的電子郵件模板我有一個令牌一樣,{{my.emailBody:default=Body}}我想從我的PHP代碼我自定義的電子郵件內容更換令牌,如何在PHP中使用REST API替換電子郵件campain上的標記?

這是我的代碼,

$sample = new SendSampleEmail(); 
$sample->id = 11111; 
$sample->emailAddress = "[email protected]"; 
print_r($sample->postData()); 

class SendSampleEmail{ 
    private $host = "https://AAA-AAA-121.mktorest.com"; 
    private $clientId = "dxxxxxxxxxxxxxxxxxxxxx1"; 
    private $clientSecret = "Sxxxxxxxxxxxxxxxxxxxxxxxxxxxxe"; 
    public $id; //id of to delete 
    public $emailAddress;//email address to send to 
    public $textOnly;//boolean option to send text only version 
    public $leadId;// id of lead to impersonate 

    public function postData(){ 
    $url = $this->host . "/rest/asset/v1/email/" . $this->id . "/sendSample.json?access_token=" . $this->getToken(); 
    $requestBody = "&emailAddress=" . $this->emailAddress; 
    if (isset($this->textOnly)){ 
     $requestBody .= "&textOnly=" . $this->textOnly; 
    } 
    if (isset($this->leadId)){ 
     $requestBody .= "&leadId=" . $this->leadId; 
    } 
    //print_r($requestBody); 

    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json')); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); 
    curl_getinfo($ch); 
    $response = curl_exec($ch); 
    return $response; 
    } 

    private function getToken(){ 
    $ch = curl_init($this->host . "/identity/oauth/token?grant_type=client_credentials&client_id=" . $this->clientId . "&client_secret=" . $this->clientSecret); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json',)); 
    $response = json_decode(curl_exec($ch)); 
    curl_close($ch); 
    $token = $response->access_token; 
    return $token; 
    } 
} 

使用此代碼我可以成功觸發電子郵件,但我怎樣才能替換令牌值{{my.emailBody:default=Body}}

回答

1

我有同樣的問題,我試圖從REST API使用資產令牌:http://developers.marketo.com/rest-api/assets/tokens/ 來修改令牌的值,但它是我不能使它工作的唯一端點。請讓我知道,如果你能使它工作。

但是,我用SOAP API來解決這個問題:

您創建包含要修改令牌和電子郵件的Marketo計劃內從的Marketo批活動想使用該令牌發送。

的SOAP API將安排活動運行(您可以設置當前時間立即運行),你可以設置令牌值:

public function schedule_campaign($program_name,$campaign_name,$token_name,$token_value) 
{ 
    $params = new stdClass(); 
    $params->programName = $program_name; 
    $params->campaignName = $campaign_name; 
    $dtzObj = new DateTimeZone("America/New_York"); 
    $dtObj = new DateTime('now', $dtzObj); 
    $params->campaignRunAt = $dtObj->format(DATE_W3C); 

    $token = new stdClass(); 
    $token->name = "{{my.".$token_name."}}"; 
    $token->value = $token_value; 

    $params->programTokenList = array("attrib" => $token); 
    $params = array("paramsScheduleCampaign" => $params); 

    $soapClient = new SoapClient(MARKETO_SOAP_ENDPOINT ."?WSDL", self::$auth_options); 
    try 
    { 
     $response = $soapClient->__soapCall('scheduleCampaign', $params, self::$auth_options, self::$auth_header); 
     return true; 
    } 
    catch(Exception $ex) { 
     return false; 
    } 
} 

UPDATE: 我已經找到了使用REST API更新/替換令牌的方法:

public function create_token($folder_id,$name,$content,$folder_type = 'Program') 
    { 
     $folder_id = intval($folder_id); 
     $endpoint = 'rest/asset/v1/folder/'.$folder_id.'/tokens'; 
     $url = $this->url . $endpoint . ".json?access_token=" . self::$token."&folderType=".$folder_type."&name=".$name."&type=". urlencode('rich text')."&value=".urlencode($content); 

     $ch = curl_init($url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: x-www-form-urlencoded')); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     $response = curl_exec($ch); 
     curl_close($ch); 
     return json_decode($response); 
    } 
1

令牌更換隻適用於請求市場活動和時間表活動API,您不能用發送示例電子郵件API替換我的令牌。

+0

我該怎麼做?任何示例代碼? –

相關問題