【发布时间】:2016-09-04 06:42:54
【问题描述】:
我一直在尝试 v3 中的 mailchimp API。由于我使用了 php,所以我在开始时遇到了一些麻烦,但现在一切都很顺利。
我正在尝试一次性将多个订阅者添加到我的列表中。
我看过了:https://devs.mailchimp.com/blog/batch-operations-and-put-in-api-v3-0/
并尝试了以下代码:
<?php
$apiKey = "apikey";
$listId = "listid";
$memberId = md5(strtolower("mymail@gmail.com"));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://'. $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId .'/members';
$batchurl = 'https://'. $dataCenter . '.api.mailchimp.com/3.0/batches';
$filename = "test_csv.csv";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$user_info = str_getcsv($contents, ";");
$ch = curl_init($batchurl);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey:'.$apiKey);
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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
$array = array();
for ($i = 1; $user_info[$i]; $i++) {
$array[] = array(
"method" => "PUT",
"path" => 'lists/'.$listId.'members/'.md5(trim($user_info[$i])),
"body" => '{"email_address" => '.trim($user_info[$i]).',"status" => "subscribed"}'
);
}
$bck = '{"operations": '.json_encode($array).'}' ;
curl_setopt($ch, CURLOPT_POSTFIELDS, $bck);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
但遗憾的是,这段代码没有返回错误。仅:
string(624) "{"id":"af07a55fea","status":"pending","total_operations":0,"finished_operations":0,"errored_operations":0,"submitted_at":"2016-05-09T14:46:21+00:00","completed_at":"","response_body_url":"","_links":[{"rel":"parent","href":"https://us13.api.mailchimp.com/3.0/batches","method":"GET","targetSchema":"https://us13.api.mailchimp.com/schema/3.0/Batches/Collection.json","schema":"https://us13.api.mailchimp.com/schema/3.0/CollectionLinks/Batches.json"},{"rel":"self","href":"https://us13.api.mailchimp.com/3.0/batches/af07a55fea","method":"GET","targetSchema":"https://us13.api.mailchimp.com/schema/3.0/Batches/Instance.json"}]}"
遗憾的是,我不完全理解,为什么要等待?我已经尝试在生成的 id 上获取请求,但遗憾的是,无论我事先等待了多长时间,我总是说“待定”。
有人遇到过同样的问题吗?可以做些什么来使上面的代码工作?
提前致谢!
编辑1:根据TooMuchPete给出的第一个答案进行更正。
【问题讨论】:
-
这些一次运行一个,顺便说一下,所以如果你有一堆它们被发送进来,它们都将等待轮到它们。如果您没有看到任何与
pending不同的情况,您应该联系客户支持,看看发生了什么。 -
我在节点中做了一个非常相似的操作,我遇到了同样的问题。如果您注意到 total_operations 为 0,我不知道为什么。但是我得到了同样的东西。我还在没有区别的成员之后添加了电子邮件地址的 md5 哈希。
-
我认为 total_operations: 0 意味着我们不允许进行操作,这可以解释我们遇到的麻烦,但这只是一个理论。到目前为止,我的业务也没有启动。
-
作为一个临时修复,我能够在数组的 forEach 循环中发出单个请求。只需使用诸如超时功能之类的东西来确保您不会收到一堆超时请求。既不理想也不是好的做法,但至少现在我的清单已经填写完毕。不过,仍然有兴趣看看是否有人可以帮助您解决问题。
-
@LamaDelRay 你认为这是 MailChimp 端的问题吗?我也有同样的问题。