hubspot/api-client

Hubspot API 客户端

11.3.0 2024-08-15 08:27 UTC

README

Latest Packagist Version Total Downloads

PHP HubSpot API v3 SDK(客户端)文件

安装

composer require hubspot/api-client

要求

当前包的要求是

PHP >= 7.4

示例应用

请查看我们的 示例应用

快速开始

使用访问令牌实例化 API 客户端时使用工厂

$hubspot = \HubSpot\Factory::createWithAccessToken('your-access-token');

您需要创建一个 私有应用 来获取您的访问令牌,或者您可以获取 OAuth2 访问令牌。

使用开发者 API 密钥实例化 API 客户端时使用工厂

$hubspot = \HubSpot\Factory::createWithDeveloperApiKey('your-developer-apikey');

您还可以将自定义客户端传递给工厂

$client = new \GuzzleHttp\Client([...]);

$hubspot = \HubSpot\Factory::createWithAccessToken('your-access-token', $client);

更改基本路径

$config = new \GuzzleHttp\Config();
$config->setBasePath('*');
$config->setAccessToken('*');
$config->setDeveloperApiKey('*');

$hubspot = \HubSpot\Factory::create(null, $config);

API 客户端带有中间件以实现速率和并发限制

它提供了在失败请求(状态码为 429 或 500)时启用重试的能力。请注意,使用 OAuth 的应用程序每 10 秒只能有 100 次请求限制。

$handlerStack = \GuzzleHttp\HandlerStack::create();
$handlerStack->push(
    \HubSpot\RetryMiddlewareFactory::createRateLimitMiddleware(
        \HubSpot\Delay::getConstantDelayFunction()
    )
);

$handlerStack->push(
    \HubSpot\RetryMiddlewareFactory::createInternalErrorsMiddleware(
        \HubSpot\Delay::getExponentialDelayFunction(2)
    )
);

$client = new \GuzzleHttp\Client(['handler' => $handlerStack]);

$hubspot = \HubSpot\Factory::createWithAccessToken('your-access-token', $client);

获取联系人页面

$response = $hubspot->crm()->contacts()->basicApi()->getPage();

通过电子邮件获取联系人

$contact = $hubSpot->crm()->contacts()->basicApi()->getById('example@example.com', null, null, null, false, 'email');

搜索联系人

$filter = new \HubSpot\Client\Crm\Contacts\Model\Filter();
$filter
    ->setOperator('EQ')
    ->setPropertyName('email')
    ->setValue($search);

$filterGroup = new \HubSpot\Client\Crm\Contacts\Model\FilterGroup();
$filterGroup->setFilters([$filter]);

$searchRequest = new \HubSpot\Client\Crm\Contacts\Model\PublicObjectSearchRequest();
$searchRequest->setFilterGroups([$filterGroup]);

// Get specific properties
$searchRequest->setProperties(['firstname', 'lastname', 'date_of_birth', 'email']);

// @var CollectionResponseWithTotalSimplePublicObject $contactsPage
$contactsPage = $hubspot->crm()->contacts()->searchApi()->doSearch($searchRequest);

创建联系人

$contactInput = new \HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInput();
$contactInput->setProperties([
    'email' => 'example@example.com'
]);

$contact = $hubspot->crm()->contacts()->basicApi()->create($contactInput);

更新联系人

$newProperties = new \HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInput();
$newProperties->setProperties([
    'email' => 'updatedExample@example.com'
]);

$hubspot->crm()->contacts()->basicApi()->update($contactId, $newProperties);

存档联系人

$hubspot->crm()->contacts()->basicApi()->archive($contactId);

获取自定义对象页面

$hubspot->crm()->objects()->basicApi()->getPage(HubSpot\Crm\ObjectType::CONTACTS)

文件上传

$file = new \SplFileObject('file path');
$response = $hubspot->files()->filesApi()->upload($file, null, '/', null, null, json_encode([
    'access' => 'PRIVATE',
    'ttl' => 'P2W',
    'overwrite' => false,
    'duplicateValidationStrategy' => 'NONE',
    'duplicateValidationScope' => 'EXACT_FOLDER'
]) );

未封装端点(s)

可以直接访问 HubSpot 请求方法,如果客户端尚未实现某些端点,则可能很有用。公开的请求方法通过具有所有配置的客户端参数而受益。

$response = $hubspot->apiRequest([
    'method' => 'PUT',
    'path' => '/some/api/not/wrapped/yet',
    'body' => ['key' => 'value'],
]);

apiRequest 选项

[
    'method' => string, // Http method (e.g.: GET, POST, etc). Default value GET
    'path' => string, // URL path (e.g.: '/crm/v3/objects/contacts'). Optional
    'headers' => array, // Http headers. Optional.
    'body' => mixed, // Request body (if defaultJson set body will be transforted to json string).Optional.
    'authType' => enum(none, accessToken, hapikey, developerApiKey), // Auth type. if it isn't set it will use accessToken or hapikey. Default value is non empty auth type.
    'baseUrl' => string, // Base URL. Default value 'https://api.hubapi.com'.
    'qs' => array, // Query parameters. Optional.
    'defaultJson' => bool, // Default Json. if it is set to true it add to headers [ 'Content-Type' => 'application/json', 'Accept' => 'application/json, */*;q=0.8',]
    // and transfort body to json string. Default value true
];

获取联系人

$response = $hubspot->apiRequest([
    'path' => '/crm/v3/objects/contacts',
]);

贡献

运行规范测试

vendor/bin/phpspec run

运行单元测试

vendor/bin/phpunit ./tests