首页 > 解决方案 > 使用 php / jquery 最好地返回 Hubspot 错误

问题描述

Hubspot 正在返回错误,即当发生异常时,现有联系人作为字符串看起来像这样(转储):

客户端错误:“POST https://api.hubapi.com/contacts/v1/contact?hapikey=***”导致“409 冲突”响应:{"message":"联系人已存在","identityProfile" :{"vid":851,"identity":[{"value":"xxx.xxx@xxx.com"," (截断...)

在 PHP 或 jQuery/Javascript 中访问“message”和“vid”值的最佳方法是什么?

这是进行调用的php(在livewire组件中,但也可以很容易地成为控制器等)代码:

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());
}

这就是我(目前)在客户端处理响应的方式:

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

我希望能够做类似的事情:

$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 
.
.

你会明白的。

标签: phplaravelexceptionhubspot-api

解决方案


推荐阅读