-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can i update contact? #14
Comments
Hi @crystaldaking, Here's an example for you, I'll add it to the <?php
use Getresponse\Sdk\GetresponseClientFactory;
use Getresponse\Sdk\Operation\Contacts\GetContact\GetContact;
use Getresponse\Sdk\Operation\Contacts\UpdateContact\UpdateContact;
use Getresponse\Sdk\Operation\Model\NewContactCustomFieldValue;
use Getresponse\Sdk\Operation\Model\UpdateContact as UpdateContactModel;
require_once __DIR__ . '/../vendor/autoload.php';
// Create GetResponse client with API_KEY from environment variable
$client = GetresponseClientFactory::createWithApiKey(getenv('API_KEY'));
// it's a good practice to keep the contact ID in your system and not get it via API each time
$contactId = 'contact123';
$getContact = new GetContact($contactId);
$getResult = $client->call($getContact);
if (!$getResult->isSuccess()) {
// handle error
throw new RuntimeException('Failed to get contact, code: ' . $getResult->getResponse()->getStatusCode());
}
$contact = $getResult->getData();
// custom fields to add, with custom field ID as a key and values array as value
$newCustomFieldValues = [
'custom1' => ['18-29'],
'custom2' => ['lorem', 'ipsum'],
];
// array of custom field IDs to delete
$customFieldsToDelete = ['custom3'];
// customFieldValues in UpdateContact operation work as a "replace",
// so we need to send custom fields that we don't want to delete or update
$customFields = [];
foreach ($contact['customFieldValues'] as $customFieldValue) {
$customFieldId = $customFieldValue['customFieldId'];
if (in_array($customFieldId, $customFieldsToDelete, true)) {
// skip custom fields that should be removed from contact
continue;
}
$customFields[$customFieldId] = new NewContactCustomFieldValue($customFieldId, $customFieldValue['value']);
}
// add or update values that we want changed
foreach ($newCustomFieldValues as $newCustomFieldId => $newCustomFieldValue) {
$customFields[$newCustomFieldId] = new NewContactCustomFieldValue($newCustomFieldId, $newCustomFieldValue);
}
// we don't want to change anything, but custom fields, so we can set only that
$updateContactModel = new UpdateContactModel();
$updateContactModel->setCustomFieldValues(array_values($customFields));
$updateContact = new UpdateContact($updateContactModel, $contactId);
$updateResult = $client->call($updateContact);
if (!$updateResult->isSuccess()) {
// handle error
throw new RuntimeException('Failed to update contact, code: ' . $updateResult->getResponse()->getStatusCode());
}
echo 'Contact updated!' . PHP_EOL;
var_dump($updateResult->getData()); It's fully runnable if you just replace contact's and custom fields' IDs (and values) with data from your account and pass your API Key in the Let me know if you need clarification or any more details. A short note that everything is fine and clear is also much appreciated :) |
When using this example setNote seems not to be working. |
Hello.
Can u provide sample code for UpdateContact with UpdateContactCustomFieldValue function ?
The text was updated successfully, but these errors were encountered: