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

时间:2016-08-29 06:02:48

标签: php rest api email marketo

我在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}}

2 个答案:

答案 0 :(得分:1)

令牌替换仅适用于请求广告系列和计划广告系列API,您无法使用发送示例电子邮件API替换我的令牌。

答案 1 :(得分:1)

我遇到了同样的问题,我尝试使用REST API中的Assets Tokens: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更新/替换令牌的方法:

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);
    }