我一直在尝试使用它的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给出的第一个答案进行更正。
答案 0 :(得分:1)
所以,我发送了对mailchimp API支持的调查。你得到的反应在技术上是可以的。
您应该使用获取请求返回的批次ID来轮询批次的URL。
{"id":"af07a55fea", ...
然后在状态更改{"id":"af07a55fea","status":"finished",
Link批量API文档以获得更清晰的信息。确保引用针对批处理API的get请求的读取选项卡。
答案 1 :(得分:0)
看起来你在路径中放了太多信息。在您关联的文档中,他们会在/3.0/
之后开始他们的路径,而您似乎从http://
开始。