hubspot / api-client
Hubspot API 客户端
11.3.0
2024-08-15 08:27 UTC
Requires
- php: >=7.4
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^7.3
- guzzlehttp/psr7: ^1.7 || ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- phpspec/phpspec: ^7.1
- phpunit/phpunit: ^9.5
- dev-master
- 11.3.0
- 11.2.0
- 11.1.0
- 11.0.0
- 10.3.0
- 10.2.0
- 10.1.2
- 10.1.1
- 10.1.0
- 10.0.1
- 10.0.0
- 10.0.0-beta.3
- 10.0.0-beta.2
- 10.0.0-beta
- 9.4.0
- 9.3.0
- 9.2.2
- 9.2.1
- 9.2.0
- 9.1.0
- 9.0.1
- 9.0.0
- 8.4.1
- 8.4.0
- 8.3.1
- 8.3.0
- 8.2.1
- 8.2.0
- 8.1.2
- 8.1.1
- 8.1.0
- 8.0.0
- 7.0.0
- 6.0.1
- 6.0.0
- 5.1.1
- 5.1.0
- 5.0.0
- v4.x-dev
- 4.0.0
- 3.1.0
- 3.0.2
- 3.0.1
- 2.8.1
- 2.8.0
- 2.7.1
- 2.7.0
- 2.6.1
- 2.6.0
- 2.5.0
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.0
- v1.x-dev
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0-beta
- dev-feature/codegen
This package is auto-updated.
Last update: 2024-09-15 08:44:33 UTC
README
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