php - 使用 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
.
.
你会明白的。
解决方案
推荐阅读
- java - Jsoup.connect() 的 get 方法阻塞 UI
- node.js - 节点测试如何使用 express-jwt-permissions 保护存根中间件
- linux - 如何将 2 git 生成的文件的差异快速导出为可修补的文件
- c# - 在显示之前替换字符串的值
- git - 恢复到以前版本的 git 修订版并推送它以使其成为新的头?
- python - Python Click:setuptools下的异常处理
- python - self.close_application 添加“self.close_application()”导致程序关闭
- java - 嵌套的 For 循环计数到 1-100 和 1-1000
- git - 在本地机器上的 Git 中设置“原始”URL 以使用 AWS EC2 Ubuntu 16 实例
- java - 可以在没有迭代的情况下在 HashMap 中获取、放置和删除 elemetn 导致 ConcurrentModificationException?