APIs & Integrations

pycs
Member

Unable to update contact property using PHP

Hi,
I am trying to update a contact property using php. However the following code doesn’t seem to work:
$api_url = “https://api.hubapi.com/contacts/v1/contact/vid/".$_COOKIE[‘vid’]."/profile?hapikey=”.$hapikey;
$ch = curl_init($api_url);
$unsub_wrapper = array (
‘properties’ => array (
‘property’ => ‘email’,
‘value’ => ‘cc@gmail.com’
)
);
$unsub_wrapper_string = json_encode($unsub_wrapper);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $unsub_wrapper_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
'Content-Length: ’ . strlen($unsub_wrapper_string))
);
$response = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

The vid should be correct.

0 Upvotes
5 Replies 5
danaketh
Participant

Unable to update contact property using PHP

What response do you get? You’ll get 204 code if everything is fine, otherwise there is an error message returned. Are you sure the $_COOKIE['vid'] actually contains a value?

if (isset($_COOKIE['vid']) && !empty($_COOKIE['vid'])) {
  // Just a very simple way of making sure the contact ID is an integer
  // You can always write a proper check, for example verify not just that
  // it's a number but also that it fits the format HubSpot uses
  $contactId = (int) $_COOKIE['vid'];
} else {
  throw new \Exception('Missing contact ID');
}

// You can also use sprintf() for this to keep things looking more sexy
$api_url = "https://api.hubapi.com/contacts/v1/contact/vid/".$contactId."/profile?hapikey=".$hapikey;
$ch = curl_init($api_url);
$unsub_wrapper = array (
  'properties' => array (
    'property' => 'email',
    'value' => 'cc@gmail.com'
  )
);
$unsub_wrapper_string = json_encode($unsub_wrapper);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $unsub_wrapper_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/json',
  'Content-Length: ' . strlen($unsub_wrapper_string)
);
$response = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
0 Upvotes
kasparp
Member

Unable to update contact property using PHP

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/json',
  'Content-Length: ' . strlen($unsub_wrapper_string)
));

One ending ) was missing. Otherwise thanks @Daniel_Tlach for the code:)

pycs
Member

Unable to update contact property using PHP

I have checked the cookie for vid and it’s of the right format. I am suspecting it might be the structure of the data I am inputting that’s not correct. Thanks for the help though.

0 Upvotes
danaketh
Participant

Unable to update contact property using PHP

So, I’ve been playing a bit with this.

First of all, I’d add following right after the $response = curl_exec($ch);, so you know an error happened when it happened.

if (!$response) {
    trigger_error(curl_error($ch));
}

For me, on Windows it’s throwing SSL certificate problem, which means the request isn’t even finished.

So, for development purpose, I have worked around that with adding two options to the cURL:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

You should NEVER use these in production code but for the purpose of debugging it’s fine. And that gave me an actual response with error property cannot be missing or null.

Whole testing code looks like this:

<?php
$contactId = 00000000; // ID of a user I'm testing on - I'm using myself

// You can also use sprintf() for this to keep things looking more sexy
$api_url = sprintf(
    'https://api.hubapi.com/contacts/v1/contact/vid/%s/profile?hapikey=%s',
    $contactId,
    '<API_KEY>'
);
$ch = curl_init($api_url);
$unsub_wrapper = [
    'properties' => [
        'property' => 'email',
        'value'    => 'email@example.com'
    ]
];
$unsub_wrapper_string = json_encode($unsub_wrapper);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $unsub_wrapper_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Content-Length: ' . strlen($unsub_wrapper_string)
]);
$response = curl_exec($ch);

if (!$response) {
    trigger_error(curl_error($ch));
}

var_dump(curl_getinfo($ch, CURLINFO_HTTP_CODE));
var_dump($response);
curl_close($ch);

So, now I know some property is missing. But it doesn’t specify which property is missing, which is weird. So I went back to check I’ve done everything as it should be. Quick glance at the example body vs what I was sending and I’ve had it. properties is an array of arrays. Thus the $unsub_wrapper variables needed a bit of a modification.

$unsub_wrapper = [
    'properties' => [
        [
            'property' => 'email',
            'value'    => 'email@example.com'
        ]
    ]
];

or

$unsub_wrapper = array(
    'properties' => array(
        array(
            'property' => 'email',
            'value'    => 'email@example.com'
        )
    )
);

And bang, got return code of 204 and when I’ve checked the profile, the e-mail has been changed. Problem solved :wink:

pycs
Member

Unable to update contact property using PHP

Thanks for the help! That solved the issue. I am trying to update a property called “Newsletter Unsubscribed Date” but an error message appeared: {“isValid”:false,“message”:“Property \“newsletter unsubscribed date\” does not exist”,“error”:“PROPERTY_DOESNT_EXIST”,“name”:“newsletter unsubscribed date”}.

0 Upvotes