Studio98/hubspot / api-client
HubSpot API 客户端
dev-master
2023-12-12 22:44 UTC
Requires
- php: >=7.3
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^6.0
- guzzlehttp/psr7: ^1.7 || ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- phpspec/phpspec: ^7.1
- phpunit/phpunit: ^9.5
This package is not auto-updated.
Last update: 2024-09-19 01:33:18 UTC
README
PHP HubSpot API v3 SDK(客户端)文件
安装
composer require hubspot/api-client
需求
当前包的需求是
PHP >= 7.3
示例应用
请查看我们的示例应用
快速入门
要使用访问令牌实例化 API 客户端,请使用工厂
$hubspot = \HubSpot\Factory::createWithAccessToken('access-token');
您需要创建一个私有应用以获取您的访问令牌,或者您可以获取OAuth2访问令牌。
要使用开发者 apikey 实例化 API 客户端,请使用工厂
$hubspot = \HubSpot\Factory::createWithDeveloperApiKey('developer-apikey');
您还可以向工厂传递自定义客户端
$client = new \GuzzleHttp\Client([...]); $hubspot = \HubSpot\Factory::createWithAccessToken('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('access-token', $client);
获取联系人页面
$response = $hubspot->crm()->contacts()->basicApi()->getPage();
搜索联系人
$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' ]) );
未包装端点
可以直接访问 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