はじめに
業務でLaravelとHubspotのAPI連携を行ったため、HubspotのAPI連携のやり方を残します。
なかなかLaravelとHubspotとのAPI連携のやり方の記事がなかったため、現在LaravelとHubspotとのAPI連携を実装されている方、LaravelとHubspotとのAPI連携を検討されている方の参考になりましたら幸いです。
1. コンタクト(Contact)
こちらではコンタクトに関するエンドポイントの使い方を説明します。
URL: https://developers.hubspot.jp/docs/api/crm/contacts
① 新規作成(/crm/v3/objects/contacts)
LaravelのWebサービス上の情報からCreateエンドポイントを使用してコンタクトの新規作成を行います。
use HubSpot\Client\Crm\Contacts\ApiException;
use HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInput;
$hubspot = Factory::createWithAccessToken('ご自身のアクセストークン');
$properties1 = ['カスタムプロパティの名前' => '値'];
$simplePublicObjectInput = new SimplePublicObjectInput([
'properties' => $properties1,
]);
try {
$apiResponse = $hubspot->crm()->contacts()->basicApi()->create($simplePublicObjectInput);
return [true,"Success basic_api->create"];
} catch (ApiException $e) {
return [false, "Exception when calling basic_api->create: ". $e->getMessage()];
}
② 更新(/crm/v3/objects/contacts/{contactId})
更新時は対象のコンタクトのコンタクトID(Hubspot固有のID)を取得しないと更新ができないため、Searchエンドポイントで対象のコンタクトIDを習得してから更新を行います。
use HubSpot\Factory;
use HubSpot\Client\Crm\Contacts\ApiException;
use HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInput;
/* Searchエンドポイント用 */
use HubSpot\Client\Crm\Contacts\Model\FilterGroup;
use HubSpot\Client\Crm\Contacts\Model\PublicObjectSearchRequest;
$hubspot = Factory::createWithAccessToken('ご自身のアクセストークン');
/* 特定のプロパティ値からコンタクトIDを取得する */
$filter1 = new Filter([
'value' => '参照したい値',
'property_name' => '参照したい値が入っているカスタムプロパティ名',
'operator' => 'EQ'
]);
$filterGroup1 = new FilterGroup([
'filters' => [$filter1]
]);
$publicObjectSearchRequest = new PublicObjectSearchRequest([
'filter_groups' => [$filterGroup1],
'limit' => 100,
'after' => 0,
]);
try {
$apiResponse = $hubspot->crm()->contacts()->searchApi()->doSearch($publicObjectSearchRequest);
/* 値がjsonで返ってくるため、phpの値に変換しcontact_idを取り出す */
$resultArr = json_decode($apiResponse,true,10,null);
$total = $resultArr['total'];
if ($total > 0) {
$exist2 = array_key_exists('id', $resultArr['results'][0]);
if($exist2){
$contact_id = $resultArr['results'][0]['id'];
}
}
} catch (ApiException $e) {
return [false,"Exception when calling search_api->do_search: ". $e->getMessage()];
// echo "Exception when calling search_api->do_search: ", $e->getMessage();
}
/* 更新の処理 */
$properties1 = ['カスタムプロパティの名前' => '値'];
$simplePublicObjectInput = new SimplePublicObjectInput([
'properties' => $properties1,
]);
try {
$apiResponse = $hubspot->crm()->contacts()->basicApi()->Update('先ほど取得したcontact_id', $simplePublicObjectInput);
return [true,"Success basic_api->update"];
} catch (ApiException $e) {
return [false,"Exception when calling basic_api->update: ". $e->getMessage()];
}
2. 取引(Deal)
こちらでは取引に関するエンドポイントの使い方を説明します。
URL: https://developers.hubspot.jp/docs/api/crm/deals
① 新規作成(/crm/v3/objects/deals)
use HubSpot\Factory;
use HubSpot\Client\Crm\Deals\ApiException;
use HubSpot\Client\Crm\Deals\Model\SimplePublicObjectInput;
$hubspot = Factory::createWithAccessToken('ご自身のアクセストークン');
$properties1 = ['カスタムプロパティの名前' => '値'];
$simplePublicObjectInput = new SimplePublicObjectInput([
'properties' => $properties1,
]);
try {
$apiResponse = $hubspot->crm()->deals()->basicApi()->create($simplePublicObjectInput);
} catch (ApiException $e) {
echo "Exception when calling basic_api->create: ", $e->getMessage();
}
② 更新(/crm/v3/objects/deals/{dealId})
更新時は対象の取引のRecord ID(Hubspot固有のID)を取得しないと更新ができないため、Searchエンドポイントで対象のRecord IDを習得してから更新を行います。
use HubSpot\Factory;
use HubSpot\Client\Crm\Deals\ApiException;
use HubSpot\Client\Crm\Deals\Model\SimplePublicObjectInput;
/* Searchエンドポイント用 */
use HubSpot\Client\Crm\Deals\Model\Filter;
use HubSpot\Client\Crm\Deals\Model\FilterGroup;
use HubSpot\Client\Crm\Deals\Model\PublicObjectSearchRequest;
$hubspot = Factory::createWithAccessToken('ご自身のアクセストークン');
/* 特定のプロパティ値からRecord IDを取得する */
$filter1 = new Filter([
'value' => '参照したい値',
'property_name' => '参照したい値が入っているカスタムプロパティ名',
'operator' => 'EQ'
]);
$filterGroup1 = new FilterGroup([
'filters' => [$filter1]
]);
$publicObjectSearchRequest = new PublicObjectSearchRequest([
'filter_groups' => [$filterGroup1],
'limit' => 100,
'after' => 0,
]);
try {
$apiResponse = $hubspot->crm()->deals()->searchApi()->doSearch($publicObjectSearchRequest);
/* 値がjsonで返ってくるため、phpの値に変換しcontact_idを取り出す */
$resultArr = json_decode($apiResponse,true,10,null);
$total = $resultArr['total'];
if ($total > 0) {
$exist2 = array_key_exists('id', $resultArr['results'][0]);
if($exist2){
$deal_id = $resultArr['results'][0]['id'];
}
}
} catch (ApiException $e) {
return [false,"Exception when calling search_api->do_search: ". $e->getMessage()];
// echo "Exception when calling search_api->do_search: ", $e->getMessage();
}
/* 更新処理 */
$properties1 = ['カスタムプロパティの名前' => '値'];
$simplePublicObjectInput = new SimplePublicObjectInput([
'properties' => $properties1,
]);
try {
$apiResponse = $hubspot->crm()->deals()->basicApi()->update('先ほど取得したdeal_id', $simplePublicObjectInput);
} catch (ApiException $e) {
echo "Exception when calling basic_api->update: ", $e->getMessage();
}
3. 商品項目(Line_Items)
こちらでは商品項目に関するエンドポイントの使い方を説明します。
URL: https://developers.hubspot.jp/docs/api/crm/line-items
Hubspotには製品と商品項目の2種類の製品があります。
製品と商品項目の違いは、
製品 ⇒ 製品ライブラリーで作成でき、取引と関連付けすると商品項目になる。
商品項目 ⇒ 個別の見積もり上に作成した商品項目は製品ライブラリーには追加されない。
今回は商品項目(Line_Items)のエンドポイントの説明をします。
① 新規作成(/crm/v3/objects/line_items)
use HubSpot\Factory;
use HubSpot\Client\Crm\LineItems\ApiException;
use HubSpot\Client\Crm\LineItems\Model\SimplePublicObjectInput;
$hubspot = Factory::createWithAccessToken('ご自身のアクセストークン');
$properties1 = ['カスタムプロパティの名前' => '値'];
$simplePublicObjectInput = new SimplePublicObjectInput([
'properties' => $properties1,
]);
try {
$apiResponse = $hubpsot->crm()->lineItems()->basicApi()->create($simplePublicObjectInput);
} catch (ApiException $e) {
echo "Exception when calling basic_api->create: ", $e->getMessage();
}
② 更新(/crm/v3/objects/line_items)
use HubSpot\Factory;
use HubSpot\Client\Crm\LineItems\ApiException;
use HubSpot\Client\Crm\LineItems\Model\SimplePublicObjectInput;
/* Searchエンドポイント用 */
use HubSpot\Client\Crm\LineItems\Model\Filter;
use HubSpot\Client\Crm\LineItems\Model\FilterGroup;
use HubSpot\Client\Crm\LineItems\Model\PublicObjectSearchRequest;
$hubspot = Factory::createWithAccessToken('ご自身のアクセストークン');
/* 特定のプロパティ値からhs_object_idを取得する */
$filter1 = new Filter([
'value' => '参照したい値',
'property_name' => '参照したい値が入っているカスタムプロパティ名',
'operator' => 'EQ'
]);
$filterGroup1 = new FilterGroup([
'filters' => [$filter1]
]);
$publicObjectSearchRequest = new PublicObjectSearchRequest([
'filter_groups' => [$filterGroup1],
'limit' => 100,
'after' => 0,
]);
$properties1 = ['カスタムプロパティの名前' => '値'];
$simplePublicObjectInput = new SimplePublicObjectInput([
'properties' => $properties1,
]);
try {
$apiResponse = $hubspot->crm()->lineItems()->searchApi()->doSearch($publicObjectSearchRequest);
/* 値がjsonで返ってくるため、phpの値に変換しcontact_idを取り出す */
$resultArr = json_decode($apiResponse,true,10,null);
$total = $resultArr['total'];
if ($total > 0) {
$exist2 = array_key_exists('id', $resultArr['results'][0]);
if($exist2){
$object_id = $resultArr['results'][0]['id'];
}
}
}catch (ApiException $e) {
echo "Exception when calling search_api->do_search: ", $e->getMessage();
}
/* 更新処理 */
$properties1 = ['カスタムプロパティの名前' => '値'];
$simplePublicObjectInput = new SimplePublicObjectInput([
'properties' => $properties1,
]);
try {
$apiResponse = $hubspot->crm()->lineItems()->basicApi()->update('先ほど取得したobject_id', $simplePublicObjectInput);
} catch (ApiException $e) {
echo "Exception when calling basic_api->update: ", $e->getMessage();
}