Skip to content
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

Open
crystaldaking opened this issue Apr 17, 2020 · 2 comments
Open

How can i update contact? #14

crystaldaking opened this issue Apr 17, 2020 · 2 comments
Assignees

Comments

@crystaldaking
Copy link

Hello.
Can u provide sample code for UpdateContact with UpdateContactCustomFieldValue function ?

@maciejtarnowski maciejtarnowski self-assigned this Apr 22, 2020
@maciejtarnowski
Copy link

Hi @crystaldaking,

Here's an example for you, I'll add it to the examples/ directory soon:

<?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 API_KEY environment variable.

Let me know if you need clarification or any more details.

A short note that everything is fine and clear is also much appreciated :)

@pdw-studio
Copy link

When using this example setNote seems not to be working.
Also is there any option to update phone number as setPhone is not defined?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants