我一直在尝试它的 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 给出的第一个答案进行更正。