使用cURL和Mailchimp API v3更新列表中的订户

时间:2015-05-29 14:10:33

标签: php curl mailchimp mailchimp-api-v3.0

我在下面的代码中将用户添加到Mailchimp中的预先存在的列表中。

$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');

此代码仅将用户添加到列表中,当我尝试将同一用户的详细信息添加两次时,会在第二次尝试时抛出以下错误:

  

test@user.com已经是列表成员。使用PATCH更新现有成员。

如何使用PATCH更新用户详细信息?我不知道在哪里指定它。

3 个答案:

答案 0 :(得分:8)

我想出了我出错的地方。当用户最初添加到列表时,响应提供ID。我需要将ID存储在我的数据库中,并附上这些人的详细信息,并在我想要在Mailchimp列表中更新用户详细信息时调用我的网址中的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");

答案 1 :(得分:3)

您正在寻找the CURLOPT_CUSTOMREQUEST option in cURL

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

但是,由于这是现在问你如何使用内置cURL库的第二个问题,因此可能值得使用更好的东西。如果您使用的是PHP 5.4或更高版本,我建议使用GuzzlePHP Requests也非常好,并且适用于PHP 5.3。

答案 2 :(得分: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 = 'faisal.im048@gmail.com';
}

$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;
}