2016-05-09 142 views
1

我一直在嘗試它的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給出的第一個答案進行更正。

+0

順便說一下,這些軟件一次只能運行一個,所以如果你有一堆它們被髮送進來,它們都會等待,直到輪到它們。如果你沒有看到任何懸而未決的問題,你應該聯繫客戶支持並瞭解具體情況。 – TooMuchPete

+0

我正在做一個非常類似的操作節點,我遇到了同樣的問題。如果你注意到total_operations是0,我不知道爲什麼。但是我得到同樣的東西。我也在會員之後添加了電子郵件地址的md5散列,這些散列沒有任何區別。 – mikedklein

+0

我認爲total_operations:0意味着我們不被允許進行操作,這將解釋我們陷入的麻煩,但那只是一個理論。到目前爲止,我的行動也沒有啓動。 – LamaDelRay

回答

1

因此,我向mailchimp API支持發送了一個查詢。你所得到的迴應在技術上是可以的。

您應該使用獲取請求返回的批次ID輪詢批處理的URL。

{"id":"af07a55fea", ...

然後取消輪詢當狀態改變{"id":"af07a55fea","status":"finished",

Link的批次API文檔更加清晰。確保針對批量API的獲取請求的引用標籤。

+0

所以我應該做異步檢查,以確保事情發生?無論如何感謝您的答案,我會嘗試這種方式! – LamaDelRay

+0

我設法使其基於您鏈接的內容工作。非常感謝您的寶貴時間! – LamaDelRay

+0

@mikedklein有沒有一種快速的方法從以前發送的批量請求中獲取id(有點像[mysql_insert_id](http://php.net/manual/en/function.mysql-insert-id.php)命令) ? – adamdesign

0

它看起來像你在道路上放了太多的信息。從您鏈接的文檔中,他們在/3.0/之後開始他們的路徑,並且您的似乎以http://開頭。

+0

編輯我的代碼的結果,它是平滑的,但仍然沒有改變迄今爲止的結果。感謝您指出這個錯誤! – LamaDelRay