How to choose identifierType using updateContact with php?

Hello, could you please explain how to choose the identifierType in php, i would like to use ext_id.

Maybe you can extend the php example Update a contact

<?php
require_once(__DIR__ . '/vendor/autoload.php');

$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'YOUR API KEY');

$apiInstance = new SendinBlue\Client\Api\ContactsApi(
    new GuzzleHttp\Client(),
    $config
);
$identifier = 'example@example.com';
$updateContact = new \SendinBlue\Client\Model\UpdateContact();

$updateContact['attributes'] = array('EMAIL'=>'example2@example2.com', 'FIRSTNAME'=>'John Doe');

try {
    $apiInstance->updateContact($identifier, $updateContact);
} catch (Exception $e) {
    echo 'Exception when calling ContactsApi->updateContact: ', $e->getMessage(), PHP_EOL;
}
?>

Many thanks in advance

Hi @tka , thanks for posting on this community.

Have you already tried any calls, and did you get any error messages?

Hi @ahudavert , thanks for your reaction.

When I try

$apiInstance->updateContact($identifier, $updateContact);

with my EXT_ID as $identifier. The result is a 404 « Contact does not exist ».
Of course, because the function does not know, that I want to use identifierType ext_id.

How could I tell the updateContact function, that I want to use identifierType ext_id?

Hi @tka ,

I just checked the doc here: https://developers.brevo.com/reference/updatecontact

From what I see you can use identifierType=ext_id ; what do you think?

Hi @ahudavert , could you please give me a full php example?

$apiInstance->updateContact($identifier, $updateContact);

Where exactly should I fill in identifierType=ext_id?

Excuse me, @ahudavert , but the problem is not solved.
Maybe you did misunderstand me, but I explicitly asked for the PHP function.

So how could the identifier type be chosed in PHP updateContact function?

$apiInstance->updateContact($identifier, $updateContact);

@tka ok I get it.

I’d say:
$updateContact['attributes'] = array('EXT_ID'=>'xxxx');

Did you try it this way?

Thanks, @ahudavert,

this works and will change the EXT_ID of the contact myemail@example.com

$identifier = 'myemail@example.com';
$updateContact['attributes'] = array('EXT_ID'=>'123456789');
$apiInstance->updateContact($identifier, $updateContact);

this will not work

$identifier = '123456789';
$updateContact['attributes'] = array('EXT_ID'=>'123456789', 'EMAIL' => 'mynewemail@somewhere.com');
$apiInstance->updateContact($identifier, $updateContact);

In my case i only know the EXT_ID of the contact, i don’t know the EMAIL
I can show you, how the curl request would look like

curl --request PUT \
     --url 'https://api.brevo.com/v3/contacts/123456789?identifierType=ext_id' \
     --header 'accept: application/json' \
     --header 'api-key: xkeysib-564df6ad46df464d6fd9ef4d64fd64f6dsdf' \
     --header 'content-type: application/json' \
     --data '
{
  "attributes": {
    "EMAIL": "mynewemail@somewhere.com"
  }
}

The curl request works fine, It would be great, if I could do this request with PHP

Good morning,

I invite you to try the code below, it works on my side

Good luck :wink:

`<?php
$first_name=‹ Jean ›;
$last_name=‹ Paul ›;
$ext_ID=17;
$email=‹ my@email.com ›;

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => « https://api.brevo.com/v3/contacts/$ext_ID?identifierType=ext_id »,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => «  »,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => « PUT »,
CURLOPT_POSTFIELDS => json_encode([
‹ attributes › => [
‹ first_name › => $first_name,
‹ last_name › => $last_name,
‹ email › => $email

],
'emailBlacklisted' => false,
'listIds' => [
    40
]

]),
CURLOPT_HTTPHEADER => [
« accept: application/json »,
« content-type: application/json;charset=utf-8 »,
« api-key: MYHAPPYKEY »
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

echo $response;`

Hi @Phil56 , you are absolutely right, a curl_exec would be a workaround in PHP.

To be honest, I would prefer to avoid a mixmax of selfwritten curl calls and Brevo PHP SDK.

Normally I use the SDK API client for PHP that Brevo provides here

I see you are a Product Manager at Brevo @ahudavert , when do you think will the PHP API client support this feature?

It says, it is « A fully-featured PHP API client to interact with Brevo », so should’nt this functionality be implemented in the PHP client?

Good morning @tka

Please see the documentation below. You will find an answer to your question.

I hope this has been useful to you. :upside_down_face:

Philippe

-·=»★«=·-update_contact -·=»★«=·-

**updateContact**
> updateContact($identifier, $updateContact, $identifierType)

Update a contact

There are 2 ways to update a contact <br><br> Option 1- https://api.brevo.com/v3/contacts/{identifier} <br><br> Option 2- https://api.brevo.com/v3/contacts/{identifier}?identifierType={} <br> <br> Option 1 only works if identifierType is email_id (for EMAIL) or contact_id (for ID of the contact),where you can directly pass the value of EMAIL and ID of the contact.   <br><br> Option 2 works for all identifierType, use email_id for EMAIL attribute, contact_id for ID of the contact, ext_id for EXT_ID attribute, phone_id for SMS attribute, whatsapp_id for WHATSAPP attribute, landline_number_id for LANDLINE attribute

### Example
```php
<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: api-key
$config = Brevo\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'YOUR_API_KEY');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = Brevo\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('api-key', 'Bearer');
// Configure API key authorization: partner-key
$config = Brevo\Client\Configuration::getDefaultConfiguration()->setApiKey('partner-key', 'YOUR_API_KEY');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = Brevo\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('partner-key', 'Bearer');

$apiInstance = new Brevo\Client\Api\ContactsApi(
    // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
    // This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client(),
    $config
);
$identifier = "identifier_example"; // string | Email (urlencoded) OR ID of the contact OR EXT_ID attribute (urlencoded) OR its SMS attribute value OR its WHATSAPP attribute value OR its LANDLINE attribute value
$updateContact = new \Brevo\Client\Model\UpdateContact(); // \Brevo\Client\Model\UpdateContact | Values to update a contact
$identifierType = new \stdClass; // object | email_id for Email, contact_id for ID of the contact, ext_id for EXT_ID attribute, phone_id for SMS attribute, whatsapp_id for WHATSAPP attribute, landline_number_id for LANDLINE attribute

try {
    $apiInstance->updateContact($identifier, $updateContact, $identifierType);
} catch (Exception $e) {
    echo 'Exception when calling ContactsApi->updateContact: ', $e->getMessage(), PHP_EOL;
}
?>

Parameters

Name Type Description Notes
identifier string Email (urlencoded) OR ID of the contact OR EXT_ID attribute (urlencoded) OR its SMS attribute value OR its WHATSAPP attribute value OR its LANDLINE attribute value
updateContact \Brevo\Client\Model\UpdateContact Values to update a contact
identifierType object email_id for Email, contact_id for ID of the contact, ext_id for EXT_ID attribute, phone_id for SMS attribute, whatsapp_id for WHATSAPP attribute, landline_number_id for LANDLINE attribute [optional]
1 « J'aime »

Hello @Phil56 , many thanks to you, that’s exactly what I have been looking for.

I did overlook all the time, that in the composer.json file the version was restricted to 1.x.x while the latest version is a 2.x.x

So I had never seen the $identifierType parameter in my local version of the SDK API client, and believed it was not implemented yet.

Now it’s running fine, thank you and have a nice day.

Good evening* @tka

I am very happy to have been of help to you. I wish you success in your business

Best regards,

Philippe

  • it’s 5 p.m. in France