You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用hubspot-api-php关联联系人与公司时遇JSON反序列化错误

HubSpot API关联联系人和公司失败排查

技术栈

  • PHP
  • Laravel
  • HubSpot SDK与Laravel集成包装器
  • HubSpot PHP SDK:hubspot-api-php

问题描述

通过SDK调用HubSpot API可正常创建联系人和公司,但手动创建二者的关联时失败。

代码示例

// 创建新联系人
$contactInput = new \HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInputForCreate();
$contactInput->setProperties([
    'firstname' => 'Jonny',
    'lastname' => 'Cage',
    'email' => 'johnny.cage@email.com',
    'phone' => '123456',
    'company' => 'Acme inc'
]);
$contact = HubSpot::crm()->contacts()->basicApi()->create($contactInput);

// 创建新公司
$companyInput = new \HubSpot\Client\Crm\Companies\Model\SimplePublicObjectInputForCreate();
$companyInput->setProperties([
    'name' => 'Acme inc', 
    'country' => 'United Kingdom',
    'city' => 'London',
]);
$company = HubSpot::crm()->companies()->basicApi()->create($companyInput);

// 关联联系人与公司
// https://developers.hubspot.com/docs/api/crm/associations

/*
* https://github.com/HubSpot/hubspot-api-php/blob/master/codegen/Crm/Associations/V4/Model/AssociationSpec.php#L43
* @param mixed[] $data 关联属性值数组
*/
$association_spec = new \HubSpot\Client\Crm\Associations\V4\Model\AssociationSpec([
    'association_category' => \HubSpot\Client\Crm\Associations\V4\Model\AssociationSpec::ASSOCIATION_CATEGORY_HUBSPOT_DEFINED,
    'association_type_id' => 1, // 联系人与公司的主关联类型
]);
// dd($associationSpec->valid()); // 此方法返回true

/* 
* https://github.com/HubSpot/hubspot-api-php/blob/master/codegen/Crm/Associations/V4/Api/BasicApi.php#L451
* @param  string $object_type 源对象类型(必填)
* @param  string $object_id 源对象ID(必填)
* @param  string $to_object_type 目标对象类型(必填)
* @param  string $to_object_id 目标对象ID(必填)
* @param  \HubSpot\Client\Crm\Associations\V4\Model\AssociationSpec[] $association_spec 关联规则集合(必填)
* @param  string $contentType Content-Type请求头值,可查看self::contentTypes['create']获取可选值
*/
$association = Hubspot::crm()->associations()->v4()->basicApi()->create('contacts', $contact->getId(), 'companies', $company->getId(), $association_spec);

错误响应

[400] Client error: `PUT https://api.hubapi.com/crm/v4/objects/contact/xxx/associations/company/xxxxxx` resulted in a `400 Bad Request` response:

{"status":"error","message":"Invalid input JSON on line 1, column 1: Cannot deserialize value of type `java.util.HashSet (truncated...)"

问题排查与解决

错误提示说明API无法反序列化java.util.HashSet类型的值,核心原因是:SDK的create方法要求association_spec参数为**AssociationSpec对象的数组集合**,但你传入的是单个对象,导致JSON序列化格式不符合API预期。

只需将单个$association_spec包装进数组即可修复问题,修改关联代码的最后一行:

$association = Hubspot::crm()->associations()->v4()->basicApi()->create(
    'contacts', 
    $contact->getId(), 
    'companies', 
    $company->getId(), 
    [$association_spec] // 改为数组形式传入
);

调整后SDK会生成API期望的JSON格式,关联操作即可正常执行。

内容的提问来源于stack exchange,提问作者William Tonkin-Howe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 00:47:36