有关php mailchimp API的一些问题

时间:2018-11-12 15:46:41

标签: php api mailchimp-api-v3.0

我尝试使用mailchimp api进行列表管理,请问3个问题。

  1. 我如何添加一个以上的成员来列出:

    POST / lists / {list_id} / members?

  2. 我如何使用以下方法更新列表中的一位以上成员:

    PATCH / lists / {list_id} / members / {subscriber_hash}?

  3. 我如何通过mailchimp的模板广告系列通过mandril发送一封电子邮件?

我有很多成员要添加/更新。一个接一个地做可能会花费很长时间。

1 个答案:

答案 0 :(得分:0)

  

我如何添加一个以上的成员来列出:

好吧,这是一个json数组,新成员的数组(恰当地)称为members,因此对CURLOPT_POSTFIELDS设置为/ p // lists / {list_id}进行POST请求

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
    'members' => array(
        newmember,
        newmember,
        newmember,
        (...)
    )
)));

还请记住将Content-Type标头设置为application/json(当给CURLOPT_POSTFIELDS一个字符串时,curl默认将其设置为application/x-www-form-urlencoded

curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/json'));

newmember应该看起来像这样:

class Member
{
    /** @var string Email address for a subscriber */
    public $email_address;
    /** @var string Type of email this member asked to get (‘html’ or ‘text’) */
    public $email_type = "html";

    /** @var string Subscriber’s current status.  Possible Values: "subscribed" "unsubscribed" "cleaned" "pending" */
    public $status;

    /** @var Merge_Field[] */
    public $merge_fields = [];

    /** @var Interest[] Subscriber Interests*/
    public $interests = [];

    /** @var string|null If set/detected, the subscriber’s language. */
    public $language = null;

    /** @var bool VIP status for subscriber */
    public $vip = false;

    /** @var Location|null Subscriber location information. */
    public $location = null;

    /** @var string|null IP address the subscriber signed up from. */
    public $ip_signup = null;

    /** @var string|null The date and time the subscriber signed up for the list in ISO 8601 format.*/
    public $timestamp_signp = null;

    /** @var string|null The IP address the subscriber used to confirm their opt-in status. */
    public $ip_opt = null;

    /** @var string|null The date and time the subscribe confirmed their opt-in status in ISO 8601 format. */
    public $timestamp_opt = null;
    /** @var bool Whether this batch operation will change existing members’ subscription status. */
    public $update_existing=false;
}

由于容易出错,类Member也应具有validate函数,例如

public function validate(array &$errors): bool
{
    $errors = [];
    if (empty($this->email_address)) {
        $errors[] = "empty email address";
    } else {
        if (! filter_var($this->email_address, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE)) {
            $errors[] = "invalid email address (failed FILTER_VALIDATE_EMAIL)";
        }
    }
    if ($this->email_type !== "html" && $this->email_type !== "text") {
        $errors[] = "email_type is neither html nor text, which is the only 2 allowed values.";
    }
    {
        $legal_statuses = [
            "subscribed",
            "unsubscribed",
            "cleaned",
            "pending"
        ];
        if (! in_array($this->status, $legal_statuses, true)) {
            $errors[] = "invalid status. list of valid statuses: " . implode(", ", $legal_statuses);
        }
    }
    foreach ($this->merge_fields as $key => $tmp) {
        if (! is_a($tmp, 'Merge_Field', false)) {
            $errors[] = "member \"{$key}\" of merge_fields is not a Merge_Field";
        }
    }
    foreach ($this->interests as $key => $tmp) {
        if (! is_a($tmp, 'Interest', false)) {
            $errors[] = "member \"{$key}\" of interests is not a Interest";
        }
    }
    if (! is_bool($this->vip)) {
        $errors[] = "vip is not a bool.";
    }
    $validISO8601Date = function (string $value): bool {
        if (! is_string($value)) {
            return false;
        }

        $dateTime = \DateTime::createFromFormat(\DateTime::ISO8601, $value);

        if ($dateTime) {
            return $dateTime->format(\DateTime::ISO8601) === $value;
        }

        return false;
    };
    if (! empty($this->timestamp_signp)) {
        if (! $validISO8601Date($this->timestamp_signp)) {
            $errors[] = "timestamp_signup is not a valid ISO8601 timestamp";
        }
    }
    if (! empty($this->timestamp_opt)) {
        if (! $validISO8601Date($this->timestamp_opt)) {
            $errors[] = "timestamp_opt is not a valid ISO8601 timestamp";
        }
    }
    return empty($errors);
}
  

我如何使用以下方法更新列表中的一位以上成员:

  • 不要使用/ lists / {list_id} / members / {subscriber_hash}来做到这一点,这仅意味着一次更新一个订户。要一次修补多个成员,请改用/ lists / {list_id},然后将成员的update_existing变量设置为true。
  

我如何使用mailchimp的模板广告系列通过mandril发送一封电子邮件?

我对心轴一无所知,希望其他人能回答这个问题。

相关问题