我一直在嘗試它的v3中的mailchimp API。由於我使用PHP,我有一些麻煩,但現在一切順利。Mailchimp API v3:批量訂閱總是等待
我試圖將多個訂戶添加到我的列表中,一氣呵成。
我看了有:https://devs.mailchimp.com/blog/batch-operations-and-put-in-api-v3-0/
,並試圖下面的代碼:
<?php
$apiKey = "apikey";
$listId = "listid";
$memberId = md5(strtolower("[email protected]"));
$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給出的第一個答案進行更正。
順便說一下,這些軟件一次只能運行一個,所以如果你有一堆它們被髮送進來,它們都會等待,直到輪到它們。如果你沒有看到任何懸而未決的問題,你應該聯繫客戶支持並瞭解具體情況。 – TooMuchPete
我正在做一個非常類似的操作節點,我遇到了同樣的問題。如果你注意到total_operations是0,我不知道爲什麼。但是我得到同樣的東西。我也在會員之後添加了電子郵件地址的md5散列,這些散列沒有任何區別。 – mikedklein
我認爲total_operations:0意味着我們不被允許進行操作,這將解釋我們陷入的麻煩,但那只是一個理論。到目前爲止,我的行動也沒有啓動。 – LamaDelRay