APIs & Integrations

AnwarJutt98
Member

hot to best return Hubspot errors with php / jquery

Hubspot is returning errors i.e. for an exiting contact as a string looking something like this (dump) when an exception occurs:

Client error: 'POST https://api.hubapi.com/contacts/v1/contact?hapikey=***' resulted in a '409 Conflict' response: {"message":"Contact already exists","identityProfile":{"vid":851,"identity":[{"value":"xxx.xxx@xxx.com"," (truncated...)

What is the best way to access the values of "message" and "vid" in PHP or jQuery/Javascript? code

here's the php (in a livewire component but could easily be a controller etc. as well) code that makes the call:

try {
    $contact = Hubspot::contacts()->create([
        [ 'property' => 'email', 'value' => $this->email ],
        [ 'property' => 'firstname',    'value' => $this->firstname ],
        [ 'property' => 'lastname',     'value' => $this->lastname ],
        [ 'property' => 'phone',        'value' => $this->phone ],
        [ 'property' => 'gender',       'value' => $this->gender],
    ]);

    session([ 'hubspot_vid' => $contact->vid ]);
    $this->dispatchBrowserEvent('debug', 'Contact Created on Hubspot with the ID of: ' . $contact->vid);

} catch (\Exception $e) {
    dump($e->getMessage());
}

and this is how I (for the moment) process the response on the client side:

<script>
    window.addEventListener('debug', event => {
        console.log('Debug Msg.: ' + event.detail);
    })
</script>

I would love to be able to do something like:

$response = $e->getMessage(); // still the whole string returned?!
$message  = $response->message (or $response['message']) // returns: "Contact already exists" only
$id       = $response->vid (or $response['vid']) // returns: "851" only 
.
.

you'll get the idea.

0 Upvotes
2 Replies 2
TiphaineCuisset
Community Manager
Community Manager

hot to best return Hubspot errors with php / jquery

Hi @AnwarJutt98 

 

Thank you for reaching out.

 

I want to tag some of our experts on this @himanshurauthan @tjoyce @zaklein do you have any thought for @AnwarJutt98 on this? 

 

Thank you!

Best

Tiphaine


Saviez vous que la Communauté est disponible en français?
Rejoignez les discussions francophones en changeant votre langue dans les paramètres !

Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !


0 Upvotes
himanshurauthan
Thought Leader | Elite Partner
Thought Leader | Elite Partner

hot to best return Hubspot errors with php / jquery

Hi, @TiphaineCuisset thank you for tagging me in. 🙂

 

So @AnwarJutt98, in PHP if you are getting 409 for the existing contact then, you can get the vid and message by using these functions. If @e is the final response from the API endpoint then you can access the vid and message like this:

 

$response = json_decode($e['body']);

$message =  $response->message;

$vid = $contact_vid->identityProfile->vid;

 

Regards,

Digital Marketing & Inbound Expert In Growth Hacking Technology