我正在利用 SendGrid API 来管理联系人及其关联的联系人列表。参考文档here,我最初创建了一个电子邮件地址,并使用以下请求将其分配给联系人列表(XXXXX,YYYYY):
curl -X PUT "https://api.sendgrid.com/v3/marketing/contacts" \
--header "Authorization: Bearer <<YOUR_API_KEY_HERE>>" \
--header "Content-Type: application/json" \
--data '{"list_ids":["057204d4-755b-4364-a0d1-XXXXXX", "057204d4-755b-4364-a0d1-YYYYY"], "contacts": [{"email": "[email protected]"}]}'
现在,我想使用相同的请求将联系人的关联联系人列表更新为另一个 (ZZZZZ)。所以,我发出了以下命令:
curl -X PUT "https://api.sendgrid.com/v3/marketing/contacts" \
--header "Authorization: Bearer <<YOUR_API_KEY_HERE>>" \
--header "Content-Type: application/json" \
--data '{"list_ids":["057204d4-755b-4364-a0d1-ZZZZZ"], "contacts": [{"email": "[email protected]"}]}'
但是,在使用以下请求搜索电子邮件地址时:
curl -X POST "https://api.sendgrid.com/v3/marketing/contacts/search/emails" \
--header "Authorization: Bearer <<YOUR_API_KEY_HERE>>" \
--header "Content-Type: application/json" \
--data '{"emails": ["[email protected]"]}'
我注意到电子邮件地址的联系人列表没有更新为 ZZZZ;相反,所有三个列表仍然存在:
{
"result": {
"[email protected]": {
"contact": {
...........
"email": "[email protected]",
"list_ids": [
"057204d4-755b-4364-a0d1-XXXXXX",
"057204d4-755b-4364-a0d1-YYYYYY",
"057204d4-755b-4364-a0d1-ZZZZZZ"
],
..........
}
}
}
}
有人可以指导我如何正确更新与电子邮件地址关联的联系人列表吗?
我注意到电子邮件地址的联系人列表没有更新为 ZZZZ;相反,所有三个列表仍然存在
这是预料之中的 - https://docs.sendgrid.com/api-reference/contacts/add-or-update-a-contact#body说,
- array[string] - 此联系人将添加到的列表 ID 字符串数组。list_ids
并没有说该联系人将从他们已经所在的其他列表中删除。
要从列表中删除联系人,您需要前往 via 列表端点,具体来说是
/v3/marketing/lists/{id}/contacts
。您需要发出 DELETE
请求,并传递一个参数 contact_ids
,其中包含要从列表中删除的以逗号分隔的联系人 ID。
https://docs.sendgrid.com/api-reference/lists/remove-contacts-from-a-list
// Update SendGrid contact's list association
$apiKey = 'YOUR_API_KEY_HERE';
$url = 'https://api.sendgrid.com/v3/marketing/contacts';
$contactEmail = '[email protected]';
$newListIds = ['057204d4-755b-4364-a0d1-ZZZZZ'];
$data = [
'list_ids' => $newListIds,
'contacts' => [['email' => $contactEmail]]
];
$payload = json_encode($data);
$headers = [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
'Content-Length: ' . strlen($payload)
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;