我试图在使用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信息,如果没有,请告诉我。
答案 0 :(得分:0)
您的函数未为PATCH
请求设置任何数据。您需要将CURLOPT_POSTFIELDS设置为与POST请求相同:
case "PATCH":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
if ($data){
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
顺便说一句,应该对PUT
请求或需要传递数据参数的任何其他请求类型执行相同的操作。您可能还想看看我的答案here,该功能可以创建和更新列表成员。