2015-05-29 38 views
5

我有下面的代碼,將用戶添加到Mailchimp中的預先存在的列表。使用cURL和Mailchimp API v3更新列表中的訂戶

$apikey = '<api_key>'; 
     $auth = base64_encode('user:'.$apikey); 

     $data = array(
      'apikey'  => $apikey, 
      'email_address' => $email, 
      'status'  => 'subscribed', 
      'merge_fields' => array(
       'FNAME' => $name 
      ) 
     ); 
     $json_data = json_encode($data); 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, 'https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/'); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 
                'Authorization: Basic '.$auth)); 
     curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0'); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);                             

     $result = curl_exec($ch); 

     var_dump($result); 
     die('Mailchimp executed'); 

此代碼僅將用戶添加到列表中,當我嘗試添加同一用戶的詳細信息的兩倍,它拋出下面的錯誤在第二次嘗試:

[email protected] is already a list member. Use PATCH to update existing members.

如何去使用修補程序來更新用戶詳細信息?我不確定在哪裏指定它。

回答

5

我想通了哪裏出錯了。當用戶最初被添加到列表中時,響應提供了一個ID。我需要將ID存儲在我的數據庫中,並將這些人的詳細信息存儲起來,並在我想要更新Mailchimp列表中的用戶詳細信息時,參考我正在撥打的URL中的ID。

https://us2.api.mailchimp.com/3.0/lists/<list_id_goes_here>/members/<members_id_goes_here> 

感謝@TooMuchPete獲取正確的curl命令。

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); 
+4

請注意,「訂閱者ID」實際上只是MD5哈希值他們的電郵地址。 – TooMuchPete

+0

很高興知道。謝謝。 – VenomRush

+1

對於其他人在尋找如何更新現有用戶的電子郵件 - 你不能這樣做(你需要刪除舊電子郵件,然後添加新的用戶作爲一個新的)。您不能在此架構中更改標記爲「只讀」的信息:https://us9.api.mailchimp.com/schema/3.0/Lists/Members/Instance.json –

3

您正在尋找the CURLOPT_CUSTOMREQUEST option in cURL

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); 

但是,因爲這是目前在許多天問如何使用內置的cURL庫你的第二個問題,它可能會使用的東西好一點值得。如果你使用的是PHP 5.4或更高版本,我建議使用Guzzle。然而,PHP Requests也非常好,並且可以與PHP 5.3一起使用。

+0

這似乎不起作用。我收到以下錯誤: 「type」:「http://kb.mailchimp.com/api/error-docs/405-method-not-allowed」,「title」:「方法不允許」,「狀態」 :405,「detail」:「請求的方法和資源不兼容。請參閱Allow資源的可用方法頭標。」 提供的網址頁面上的鏈接導致403頁,這並沒有多大幫助。 – VenomRush

+0

另外,感謝您的建議。我會記住他們爲未來的項目。這個網站開發的框架非常古老,我想我會花更多的時間來弄清楚如何整合Guzzle,而不是它的價值。 – VenomRush

+0

評論失敗,我原來的評論中的網址是http://kb.mailchimp.com/api/error-docs/405-method-not-allowed – VenomRush

0

試試這個。它爲我工作。我正在使用這個功能。希望它能解決你的問題。

<?php 
/** 
* Created by PhpStorm. 
* User: Faisal 
* Website: www.faisal-ibrahim.info 
* Date: 2/12/2016 
* Time: 10:07 AM 
*/ 
if (isset($_POST['email'])) { 
    $email = $_POST['email']; 
} else { 
    $email = '[email protected]'; 
} 

$data    = [ 
    'email'  => $email, 
    'status' => 'subscribed', 
    'firstname' => 'Faisal', 
    'lastname' => 'Ibrahim' 
]; 
$api_response_code = listSubscribe($data); 
echo $api_response_code; 

/** 
* Mailchimp API- List Subscribe added function.In this method we'll look how to add a single member to a list using the lists/subscribe method.Also, We will cover the different parameters for submitting a new member as well as passing in generic merge field information. 
* 
* @param array $data Subscribe information Passed. 
* 
* @return mixed 
*/ 
function listSubscribe(array $data) 
{ 
    $apiKey = "cf8a1fd222a500f27f9e042449867c7c-us15";//your API key goes here 
    $listId = "e8f3f5f880";// your trageted list ID 

    $memberId = md5(strtolower($data['email'])); 
    $dataCenter = substr($apiKey, strpos($apiKey, '-') + 1); 
    $url  = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId; 
    $json  = json_encode([ 
     'email_address' => $data['email'], 
     'status'  => $data['status'], // "subscribed","unsubscribed","cleaned","pending" 
     'merge_fields' => [ 
      'FNAME' => $data['firstname'], 
      'LNAME' => $data['lastname'] 
     ] 
    ]); 

    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $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_CUSTOMREQUEST, 'PUT'); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json); 

    $result = curl_exec($ch); 
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); 

    return $httpCode; 
}