我正在使用"蓝色发送"来跟踪我的联系人。在我的整个网站中,我要么更新联系人,要么创建一个新联系人,但我从来没有将两者结合起来。我在下面分别有我的代码片段。
我如何将两者结合起来,以便如果那里已经有用户,那么它会更新它们,如果没有,那么它会创建它们?
是否可以先更新,如果失败,请创建它们?
try {
$updateContact = new SendinBlueClientModelUpdateContact($data);
$result = $SendInBlue->updateContact($email, $updateContact);
return true;
} catch (Exception $e) {
}
这是创建联系人的代码:
try {
$createContact = new SendinBlueClientModelCreateContact($data);
$result = $SendInBlue->createContact($email, $createContact);
return true;
} catch (Exception $e) {
}
您可以删除updateContact()
并仅使用createContact()
,因为这是一个可以在创建尝试期间设置的updateEnabled
选项。 此选项的目的是"便于在同一请求中更新现有联系人(updateEnabled = true(">
您可以在创建CreateContact
对象时设置此设置...
$data["updateEnabled"] = true;
$createContact = new SendinBlueClientModelCreateContact($data);
或者通过使用setUpdateEnabled()
方法...
try {
$createContact = new SendinBlueClientModelCreateContact($data);
$createContact->setUpdateEnabled(true);
$result = $SendInBlue->createContact($email, $createContact);
return true;
} catch (Exception $e) {
}
作为参考,下面是您正在使用的CreateContact
类的代码,下面是原始 SendInBlue API 的文档。