如何在 PHP 中使用 REST API 替换电子邮件活动中的令牌?

How to replace tokens on email campain using REST API in PHP ?

我在 Marketo 上有一个电子邮件活动,使用 PHP 发送电子邮件。在我的电子邮件模板上,我有一个令牌,例如 {{my.emailBody:default=Body}} 我想用我的 PHP 代码

中的自定义电子邮件内容替换该令牌

这是我的代码,

$sample = new SendSampleEmail();
$sample->id = 11111;
$sample->emailAddress = "myemail@example.com";
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}}

令牌替换仅适用于请求营销活动和计划营销活动 APIs,您不能将我的令牌替换为发送示例电子邮件 API。

我遇到了同样的问题,我尝试使用来自 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;
    }
}

更新: 我找到了使用 REST API:

获取 update/replace 令牌的方法
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);
    }