[英]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已經是列表成員。 使用PATCH更新現有成員。
如何使用PATCH更新用戶詳細信息? 我不知道在哪里指定它。
我想出了我哪里出錯了。 當用戶最初添加到列表時,響應提供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");
您正在尋找cURL中的CURLOPT_CUSTOMREQUEST
選項 。
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
但是,由於這是現在問你如何使用內置cURL庫的第二個問題,因此可能值得使用更好的東西。 如果您使用的是PHP 5.4或更高版本,我建議使用Guzzle 。 PHP請求也非常好,並且與PHP 5.3一起使用。
試試這個吧。 它對我有用。 我正在使用這個功能。 希望它能解決你的問題。
<?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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.