Mailchimp api v3:批量订阅始终处于待处理状态
Mailchimp api v3 : Batch subscription always pending
我一直在试用 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);
然而遗憾的是,这段代码returns没有错误。只有:
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 上获取请求,遗憾的是总是说 "pending",不管我事先等了多久。
有没有人遇到过同样的问题?怎样做才能使上述代码工作?
提前致谢!
编辑 1:根据 TooMuchPete 给出的第一个答案进行更正。
您似乎在路径中放置了太多信息。从您链接的文档中,它们在 /3.0/
之后开始他们的路径,而您的路径似乎从 http://
.
开始
因此,我向 mailchimp API 支持发送了询问。您得到的答复在技术上是可以的。
您应该使用获取请求返回的批次 ID 轮询 url 批次。
{"id":"af07a55fea", ...
然后状态改变时取消轮询{"id":"af07a55fea","status":"finished",
Link 到批次 API 文档以更清楚。确保针对批次 API.
引用获取请求的读取选项卡
我一直在试用 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);
然而遗憾的是,这段代码returns没有错误。只有:
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 上获取请求,遗憾的是总是说 "pending",不管我事先等了多久。
有没有人遇到过同样的问题?怎样做才能使上述代码工作?
提前致谢!
编辑 1:根据 TooMuchPete 给出的第一个答案进行更正。
您似乎在路径中放置了太多信息。从您链接的文档中,它们在 /3.0/
之后开始他们的路径,而您的路径似乎从 http://
.
因此,我向 mailchimp API 支持发送了询问。您得到的答复在技术上是可以的。
您应该使用获取请求返回的批次 ID 轮询 url 批次。
{"id":"af07a55fea", ...
然后状态改变时取消轮询{"id":"af07a55fea","status":"finished",
Link 到批次 API 文档以更清楚。确保针对批次 API.
引用获取请求的读取选项卡