Skip to content

HubSpot API PHP Client Libraries for V3 version of the API

License

Notifications You must be signed in to change notification settings

atymic/hubspot-api-php

 
 

Repository files navigation

hubspot-api-php

PHP HubSpot API v3 SDK(Client) files and sample apps

Installation

composer require hubspot/api-client

Sample Applications can be found in sample-apps folder

Quickstart

To instantiate API Client using HubSpot API Key use Factory:

$hubSpot = \HubSpot\Factory::createWithApiKey('api-key');

or using OAuth2 access token:

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

also you can pass custom client to Factory:

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

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

API Client comes with Middleware for implementation of Rate and Concurrent Limiting.

It provides an ability to turn on retry for failed requests with statuses 429 or 500. Please note that Apps using OAuth are only subject to a limit of 100 requests every 10 seconds.

$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);

Get contacts page:

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

Search for a contact:

$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]);

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

Create a contact:

$contactInput = new \HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInput();
$contactInput->setProperties($_POST);

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

Update a contact:

$newProperties = new \HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInput();
$newProperties->setProperties($_POST);

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

Contributing

Run spec tests

vendor/bin/phpspec run

Run unit tests

vendor/bin/phpunit ./tests

About

HubSpot API PHP Client Libraries for V3 version of the API

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages

  • PHP 99.6%
  • Other 0.4%