MailChimp API v3 - 无法修补/更新?

MailChimp API v3 - Unable to PATCH/ Update?

我试图在使用 API 将我的网站注册到 MailChimp 列表时简单地添加/删除(订阅/取消订阅而不是删除)电子邮件地址。当用户在我的网站上获得批准时,如果勾选了正确的复选框,则通过 API 成功添加电子邮件地址。但是,我终生无法取消订阅工作。我想我在使用 PATCH 方法时遇到了问题。代码没有错误,但是列表中的用户状态不会更改为取消订阅,如果我回显 API 调用,我只会得到以下信息:

{"id":"EMAIL_HASH","email_address":"EMAIL_UNHASHED","unique_email_id":"3dae8dd13a","email_type":"html","status":"subscribed","merge_fields":{"FNAME":"","LNAME":"","MMERGE3":"","MMERGE4":"","MMERGE5":"","MMERGE6":"","MMERGE7":"","MMERGE8":"","MMERGE9":"","MMERGE10":""},"interests":{"edf7fefb79":false},"stats":{"avg_open_rate":0,"avg_click_rate":0},"ip_signup":"","timestamp_signup":"","ip_opt":"77.68.14.17","timestamp_opt":"2018-11-23T14:02:27+00:00","member_rating":2,"last_changed":"2018-11-23T14:02:27+00:00","language":"","vip":false,"email_client":"","location":{"latitude":0,"longitude":0,"gmtoff":0,"dstoff":0,"country_code":"","timezone":""},"tags_count":0,"tags":[],"list_id":"LIST_ID","_links":[{"rel":"self","href":"https://us6.api.mailchimp.com/3.0/lists/LIST_ID/members/EMAIL_HASH","method":"GET","targetSchema":"https://us6.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Response.json"},{"rel":"parent","href":"https://us6.api.mailchimp.com/3.0/lists/LIST_ID/members","method":"GET","targetSchema":"https://us6.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/CollectionResponse.json","schema":"https://us6.api.mailchimp.com/schema/3.0/CollectionLinks/Lists/Members.json"},{"rel":"update","href":"https://us6.api.mailchimp.com/3.0/lists/LIST_ID/members/EMAIL_HASH","method":"PATCH","targetSchema":"https://us6.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Response.json","schema":"https://us6.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/PATCH.json"},{"rel":"upsert","href":"https://us6.api.mailchimp.com/3.0/lists/LIST_ID/members/EMAIL_HASH","method":"PUT","targetSchema":"https://us6.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Response.json","schema":"https://us6.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/PUT.json"},{"rel":"delete","href":"https://us6.api.mailchimp.com/3.0/lists/LIST_ID/members/EMAIL_HASH","method":"DELETE"},{"rel":"activity","href":"https://us6.api.mailchimp.com/3.0/lists/LIST_ID/members/EMAIL_HASH/activity","method":"GET","targetSchema":"https://us6.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Activity/Response.json"},{"rel":"goals","href":"https://us6.api.mailchimp.com/3.0/lists/LIST_ID/members/EMAIL_HASH/goals","method":"GET","targetSchema":"https://us6.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Goals/Response.json"},{"rel":"notes","href":"https://us6.api.mailchimp.com/3.0/lists/LIST_ID/members/EMAIL_HASH/notes","method":"GET","targetSchema":"https://us6.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Notes/CollectionResponse.json"},{"rel":"delete_permanent","href":"https://us6.api.mailchimp.com/3.0/lists/LIST_ID/members/EMAIL_HASH/actions/delete-permanent","method":"POST"}]}

这看起来更像是一堆可能的操作,而不是对我的呼吁的回应。不用说 LIST_ID 和 EMAIL_HASH 是准确的并且不在最终代码中。

curl 选项如下,我是否可能遗漏了一些需要修补而不是 POST 的东西?

$curl = curl_init();

switch ($method)
{
    case "POST":
        curl_setopt($curl, CURLOPT_POST, 1);

        if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        break;
    case "PUT":
        curl_setopt($curl, CURLOPT_PUT, 1);
        break;
    case "PATCH":
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
        break;
    default:
        if ($data)
            $url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_USERPWD, "user:API_KEY");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);

任何帮助将不胜感激,因为广泛的谷歌搜索没有返回这个问题,主要是人们试图重新添加已删除的订阅者,这显然不是。

$url = 'https://us6.api.mailchimp.com/3.0/lists/LIST_ID/members/'.md5(strtolower($email));

$api_data = array(
    'status'        =>  'unsubscribed'
);

$api_data = json_encode($api_data);

$test_call = CallAPI("PATCH","$url",$api_data);

PHP使用自定义函数调用API。我想我已经在函数内部包含了相关的卷曲信息,但如果没有,请告诉我。

您的函数没有为 PATCH 请求设置任何数据。您需要设置 CURLOPT_POSTFIELDS 与 POST 请求相同:

case "PATCH":
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
    if ($data){
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    break;

顺便说一句,对于 PUT 请求或您需要传递数据参数的任何其他请求类型,也应执行相同的操作。您可能还想查看我的回答 ,了解可以创建和更新列表成员的函数。