hubspot/hubspot-php

HubSpot PHP API客户端

5.2.0 2024-02-06 09:59 UTC

README

Version Total Downloads Build Status License

HubSpot是一款市场营销、销售和服务软件,它可以帮助您的业务在不妥协的情况下增长。因为“对业务有利”也应该是“对客户有利”。

此库仅支持旧版API

请考虑切换到最新API

设置

Composer

composer require "hubspot/hubspot-php"

示例应用程序

链接

快速入门

使用工厂的示例

以下所有示例都假设此步骤。

$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

您可以在这里找到有关oauth2访问令牌的更多信息,以及在这里找到有关私有应用程序访问令牌的更多信息。

注意:您可以通过在客户端创建过程中传递以下选项来防止此包提供的任何错误处理:(也适用于Factory::create()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
);

http_errors设置为false,您将不会收到任何异常,但只会收到纯响应。有关可能选项,请参阅http://docs.guzzlephp.org/en/latest/request-options.html

API客户端包含用于实现速率和并发限制的中间件

它提供了在失败的请求状态为429或500时进行重试的能力。您可以在这里了解更多关于在HubSpot API速率限制内工作的信息。

$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("test@hubspot.com");

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

数据处理非常简单!

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'};

或者如果您喜欢使用数组访问?

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'];

现在,响应方法实现了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'
);

或使用工厂

<?php

require 'vendor/autoload.php';

use SevenShores\Hubspot\Utils;

$authUrl = Utils::getFactory()->oAuth2()->getAuthUrl(
    'clientId',
    'http://localhost/callaback.php',
    'contacts'
);

状态

如果您看到不计划中的某些内容,您希望添加的内容,请提交问题,我很可能将其添加。

  • 分析API
  • 日历API:已更新
  • 公司API:已更新
  • 公司属性API:已更新
  • 联系人API:已更新
  • 联系人列表API:已更新
  • 联系人属性API:已更新
  • Conversations实时聊天小部件API(前端)
  • CMS博客API(博客):已更新
  • CMS博客作者API(BlogAuthors):已更新
  • CMS博客评论API(BlogComments)
  • CMS博客文章API(BlogPosts)
  • CMS博客主题API(BlogTopics)
  • CMS域名API
  • CMS文件API(Files)
  • CMS HubDB API(HubDB):已更新
  • CMS布局API
  • CMS页面发布API(Pages)
  • CMS网站地图
  • CMS网站搜索API
  • CMS模板API
  • CMS URL映射API
  • CRM关联API
  • CRM扩展API
  • CRM对象属性API(ObjectProperties)🆕
  • CRM管道API(CrmPipelines)
  • 交易API
  • 交易管道API:已弃用
  • 交易属性API:已更新
  • 电子商务桥API:已更新
  • 电子邮件订阅API:已更新
  • 电子邮件事件API:已更新
  • 参与度API
  • 事件API
  • 表单API:已更新
  • 行项目API 🆕
  • 营销电子邮件API
  • 所有者API:已更新
  • 产品API 🆕
  • 社交媒体API
  • 票务API
  • 时间线API :已更新
  • 追踪代码API
  • 交易电子邮件API
  • 工作流程API :已更新
  • Webhooks API