已订阅会员的Mailchimp API状态200 [英] Mailchimp API status 200 for members who are already subscribed

查看:58
本文介绍了已订阅会员的Mailchimp API状态200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码正在工作并成功订阅新成员.但是,即使电子邮件地址已被预订,状态也会返回200.我检查了$ member_id是否正确地进行了哈希处理,尽管值相同,但仍返回200.

The following code is working and subscribing new members successfully. However, the status returns 200 even if the email address is already subscribed. I have checked that $member_id is hashing correctly, and despite identical values it still returns 200.

$result = array(
    'status' => sync_mailchimp($data)
);

var_dump($result) // 200

function sync_mailchimp($data) {

    // Setup our Mailchimp info
    $api_key = 'xxxxx';
    $list_id = 'ab8abde5bb';

    $member_id  = md5(strtolower($data['email'])); // lowercase hash of the email
    $datacenter = 'us16';
    $url = 'https://' . $datacenter . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . $member_id;

    $json = json_encode([
        'email_address' => $data['email'],
        'status'        => $data['status']
    ]);

    // Send via curl
    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);                                                                                                                 

    $result = curl_exec($ch);

    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $httpcode;
}

推荐答案

在使用类似代码段时,我也遇到了同样的问题.

I was also having the same issue while using a similar snippet.

阅读与管理订户(特别是预订地址部分),很明显,此方法不是添加新订户的正确方法.

Reading the MailChimp API 3.0 docs relating to managing subscribers (specifically the section Subscribe an Address), it was clear that this method was not the correct way to add a new subscriber.

请求类型必须为POST

The request type needs to be a POST

curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,'POST');

,并且必须发送到端点

https://< data_center> .api.mailchimp.com/3.0/lists/< list_id>/成员/

包含JSON的请求正文是相同的.

The request body containing the JSON is identical.

成功添加邮件地址时响应代码为200,列表中已存在邮件地址时响应代码为400.

The response code will be 200 when an email address is added to the list successfully, and 400 when the email address already exists on the list.

请注意,此方法不适用于更新现有订户-为此,请求类型必须为 PATCH ,并且您必须使用以哈希电子邮件地址结尾的原始端点.

Note that this method will not work for updating an existing subscriber – to do that, the request type must be PATCH, and you must use the original endpoint ending with the hashed email address.

这篇关于已订阅会员的Mailchimp API状态200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆