2015-05-29 123 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; 
}