使用 cURL 和 Mailchimp API v3 更新列表中的订阅者
Updating subscribers in a list using cURL and Mailchimp API v3
我在下面有这段代码,可以将用户添加到 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 is already a list member. Use PATCH to update existing members.
如何使用 PATCH 更新用户详细信息?我不确定在哪里指定它。
您正在寻找 the CURLOPT_CUSTOMREQUEST
option in cURL。
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
但是,由于这是您几天来询问如何使用内置 cURL 库的第二个问题,因此可能值得使用更好的东西。如果您使用的是 PHP 5.4 或更高版本,我建议 Guzzle. PHP Requests 也非常好,并且适用于 PHP 5.3.
我知道我错在哪里了。当用户最初被添加到列表时,响应会提供一个 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");
试试这个。它对我有用。我正在使用这个功能。希望它能解决你的问题。
<?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;
}
我在下面有这段代码,可以将用户添加到 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 is already a list member. Use PATCH to update existing members.
如何使用 PATCH 更新用户详细信息?我不确定在哪里指定它。
您正在寻找 the CURLOPT_CUSTOMREQUEST
option in cURL。
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
但是,由于这是您几天来询问如何使用内置 cURL 库的第二个问题,因此可能值得使用更好的东西。如果您使用的是 PHP 5.4 或更高版本,我建议 Guzzle. PHP Requests 也非常好,并且适用于 PHP 5.3.
我知道我错在哪里了。当用户最初被添加到列表时,响应会提供一个 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");
试试这个。它对我有用。我正在使用这个功能。希望它能解决你的问题。
<?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;
}