Stars: 342
Forks: 216
Pull Requests: 263
Issues: 153
Watchers: 35
Last Updated: 2023-06-07 12:44:38
HubSpot PHP API Client
License: Other
Languages: PHP, Shell, Dockerfile
Hubspot is a marketing, sales, and service software that helps your business grow without compromise. Because “good for the business” should also mean “good for the customer.”
Please consider switching to the latest API.
Composer:
composer require "hubspot/hubspot-php"
All following examples assume this step.
$hubspot = SevenShores\Hubspot\Factory::create('api-key');
// OR create with access token (OAuth2 or Private App)
$hubspot = SevenShores\Hubspot\Factory::createWithAccessToken('access-token');
// OR instantiate by passing a configuration array.
// The only required value is the 'key'
// Please note: as of November 30, 2022, HubSpot API Keys are being deprecated and are no longer supported.
$hubspot = new SevenShores\Hubspot\Factory([
'key' => 'demo',
'oauth2' => false, // default
]);
// Then you can call a resource
// When referencing endpoints, use camelCase
$hubspot->contactlists
You can find more information about oauth2 access tokens here and about private app access token here.
Note: You can prevent any error handling provided by this package by passing following options into client creation routine:
(applies also to Factory::create()
and Factory::createWithAccessToken()
)
$hubspot = new SevenShores\Hubspot\Factory(
[
'key' => 'demo',
],
null,
[
'http_errors' => false // pass any Guzzle related option to any request, e.g. throw no exceptions
],
false // return Guzzle Response object for any ->request(*) call
);
By setting http_errors
to false, you will not receive any exceptions at all, but pure responses.
For possible options, see http://docs.guzzlephp.org/en/latest/request-options.html.
It provides an ability to turn on retry for failed requests with statuses 429 or 500. You can read more about working within the HubSpot API rate limits here.
$handlerStack = \GuzzleHttp\HandlerStack::create();
$handlerStack->push(
\SevenShores\Hubspot\RetryMiddlewareFactory::createRateLimitMiddleware(
\SevenShores\Hubspot\Delay::getConstantDelayFunction()
)
);
$handlerStack->push(
\SevenShores\Hubspot\RetryMiddlewareFactory::createInternalErrorsMiddleware(
\SevenShores\Hubspot\Delay::getExponentialDelayFunction(2)
)
);
$guzzleClient = new \GuzzleHttp\Client(['handler' => $handlerStack]);
$config = [
'key' => 'access token',
'oauth2' => true,
];
$hubspot = new \SevenShores\Hubspot\Factory($config, new \SevenShores\Hubspot\Http\Client($config, $guzzleClient));
$contact = $hubspot->contacts()->getByEmail("[email protected]");
echo $contact->properties->email->value;
// Get an array of 10 contacts
// getting only the firstname and lastname properties
// and set the offset to 123456
$response = $hubspot->contacts()->all([
'count' => 10,
'property' => ['firstname', 'lastname'],
'vidOffset' => 123456,
]);
Working with the data is easy!
foreach ($response->contacts as $contact) {
echo sprintf(
"Contact name is %s %s." . PHP_EOL,
$contact->properties->firstname->value,
$contact->properties->lastname->value
);
}
// Info for pagination
echo $response->{'has-more'};
echo $response->{'vid-offset'};
or if you prefer to use array access?
foreach ($response['contacts'] as $contact) {
echo sprintf(
"Contact name is %s %s." . PHP_EOL,
$contact['properties']['firstname']['value'],
$contact['properties']['lastname']['value']
);
}
// Info for pagination
echo $response['has-more'];
echo $response['vid-offset'];
Now with response methods implementing PSR-7 ResponseInterface
$response->getStatusCode() // 200;
$response->getReasonPhrase() // 'OK';
// etc...
<?php
require 'vendor/autoload.php';
use SevenShores\Hubspot\Http\Client;
use SevenShores\Hubspot\Endpoints\Contacts;
$client = new Client(['key' => 'access token', 'oauth2' => true,]);
$contacts = new Contacts($client);
$response = $contacts->all();
foreach ($response->contacts as $contact) {
//
}
<?php
require 'vendor/autoload.php';
use SevenShores\Hubspot\Utils\OAuth2;
$authUrl = OAuth2::getAuthUrl(
'clientId',
'http://localhost/callaback.php',
'contacts'
);
or using Factory:
<?php
require 'vendor/autoload.php';
use SevenShores\Hubspot\Utils;
$authUrl = Utils::getFactory()->oAuth2()->getAuthUrl(
'clientId',
'http://localhost/callaback.php',
'contacts'
);
If you see something not planned, that you want, make an issue and there's a good chance I will add it.