8

我在下面有这段代码,可以将用户添加到 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 更新用户详细信息?我不确定在哪里指定它。

4

3 回答 3

9

我想出了我要去哪里错了。当用户最初被添加到列表中时,响应会提供一个 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");
于 2015-06-02T11:14:00.647 回答
4

您正在寻找cURL中CURLOPT_CUSTOMREQUEST选项。

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

但是,由于这是您在这么多天来询问如何使用内置 cURL 库的第二个问题,因此可能值得使用更好的东西。如果您使用的是 PHP 5.4 或更高版本,我推荐Guzzle不过, PHP Requests也非常好,并且适用于 PHP 5.3。

于 2015-05-29T21:08:44.153 回答
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;
}
于 2017-02-16T19:28:47.513 回答