MailChimp API v3 - 无法修补/更新?
Posted
技术标签:
【中文标题】MailChimp API v3 - 无法修补/更新?【英文标题】:MailChimp API v3 - Unable to PATCH/ Update? 【发布时间】:2019-04-28 05:01:58 【问题描述】:我正在尝试在使用 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。我想我已经在函数内部包含了相关的 curl 信息,但如果没有,请告诉我。
【问题讨论】:
【参考方案1】:您的函数没有为PATCH
请求设置任何数据。您需要将 CURLOPT_POSTFIELDS 设置为与 POST 请求相同:
case "PATCH":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
顺便说一句,对于PUT
请求或任何其他需要传递数据参数的请求类型,也应该这样做。您可能还想查看我的回答 here,了解可以创建和更新列表成员的函数。
【讨论】:
啊,太好了,这已经解决了问题!非常感谢大佬,感激不尽。我已将其标记为已解决并单击了赞成的东西,没有经常使用这个网站,所以如果现在有其他事情要做,现在就让我知道它已经解决了! 谢谢 - 您至少需要 15 个代表才能投票,所以现在您所能做的就是“接受”一个答案。很高兴我能帮上忙以上是关于MailChimp API v3 - 无法修补/更新?的主要内容,如果未能解决你的问题,请参考以下文章
使用maleorang v3.0 java api发送mailchimp活动
Mailchimp API V3 jQuery Ajax POST 订阅者