定制 getbrevo/brevo-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

getbrevo/brevo-php

Composer 安装命令:

composer require getbrevo/brevo-php

包简介

Official PHP SDK for the Brevo API.

README 文档

README

Brevo

fern shield php shield

Official SDK for the Brevo API.

Table of Contents

Documentation

API reference documentation is available here.

Requirements

This SDK requires PHP ^8.1.

Installation

composer require getbrevo/brevo-php

Upgrading from v4.x

v5 is a major release. Most breaking changes come from an internal effort at Brevo to make endpoints, parameters and models more self-descriptive — so names, shapes and required fields convey intent without needing to cross-reference external docs (both for developers and for AI agents working against the Brevo API).

v4.x remains supported. If you need to hold on v4 temporarily, pin it:

composer require getbrevo/brevo-php:^4.0
View v4 → v5 migration guide

Breaking changes

Companies — GetCompaniesRequest::filters renamed

  • filters is renamed to filtersAttributesName to match the wire format.
  • Existing code passing 'filters' => ... does not throw, but the filter is silently ignored server-side and the response is unfiltered. Audit every call site.

Events — Event::createBatchEvents payload shape changed

// v4
$client->event->createBatchEvents($eventsArray);

// v5
use Brevo\Event\Requests\CreateBatchEventsRequest;

$client->event->createBatchEvents(
    new CreateBatchEventsRequest(['events' => $eventsArray])
);

Balance — getActiveBalancesApi response shape changed

  • Return type changed from ?BalanceLimit to ?GetLoyaltyBalanceProgramsPidActiveBalanceResponse.

Balance — getContactBalances requires balanceDefinitionId

  • $request no longer defaults to empty; balanceDefinitionId is required.

Balance — BeginTransactionRequest::eventTime type tightened

  • Changed from ?string to ?DateTime. ISO strings now cause TypeError.

CRM — Tasks::getAllTaskTypes returns an array (?array<GetCrmTasktypesResponseItem> instead of ?GetCrmTasktypesResponse).

Email campaigns — utmIdActive renamed

  • EmailCampaigns\GetEmailCampaignResponse::utmIdActive renamed to utmId (wire key utmIDActiveutmID, type ?bool?int). Same change on GetEmailCampaignsResponseCampaignsItem.

Model fields removed: GetAccountResponse::dateTimePreferences, Webhook::channel, GetProcessResponseInfo::export, GetProcessResponse::error/createdAt/completedAt, and several ExternalFeeds response fields.

Model field types tightened:

  • Process\GetProcessResponseInfoImport count fields: ?int?string (now URLs to CSV reports) — affects invalidEmails, duplicateContactId, duplicateExtId, etc.
  • Ecommerce\CreateUpdateProductResponse::id and CreateUpdateCategoryResponse::id: ?int?string.
  • Several string date fields tightened to ?DateTime (Program::*, Reward::*, BalanceLimit::createdAt/updatedAt).

Model fields renamed: ConversationsMessageAttachmentsItem::fileNamename, ::inlineIdlink.

Custom objects — associations union flattened: UpsertrecordsRequestRecordsItemAssociationsItem is now a single class with an action: 'link' | 'unlink' discriminator. Seven old variant classes have been deleted.

Added

  • New optional fields and filters across contacts->createContact, contacts->updateContact, emailCampaigns->getEmailCampaigns, ecommerce->getProducts, and several other endpoints.
  • Tier groups support upgrade and downgrade schedule fields.

Migration from v1.x

Warning: The legacy v1.x SDK will continue to receive critical security updates but no new features. We recommend migrating to v5.x.

View migration guide

Key changes:

  • Unified Brevo client with namespaced resources
  • Standardized error handling via BrevoApiException
  • PSR-18 HTTP client support

v1.x:

use Brevo\Client\Configuration;
use Brevo\Client\Api\TransactionalEmailsApi;
use Brevo\Client\Model\SendSmtpEmail;

$config = Configuration::getDefaultConfiguration()->setApiKey('api-key', 'xkeysib-xxx');
$api = new TransactionalEmailsApi(new \GuzzleHttp\Client(), $config);

$message = new SendSmtpEmail();
$message->setSubject('First email');
$message->setTextContent('Hello world!');
$message->setSender(['name' => 'Bob Wilson', 'email' => 'bob.wilson@example.com']);
$message->setTo([['email' => 'sarah.davis@example.com', 'name' => 'Sarah Davis']]);

$api->sendTransacEmail($message);

v5.x:

use Brevo\Brevo;
use Brevo\TransactionalEmails\Requests\SendTransacEmailRequest;
use Brevo\TransactionalEmails\Types\SendTransacEmailRequestSender;
use Brevo\TransactionalEmails\Types\SendTransacEmailRequestToItem;

$brevo = new Brevo('xkeysib-xxx');

$brevo->transactionalEmails->sendTransacEmail(
    new SendTransacEmailRequest([
        'subject' => 'First email',
        'textContent' => 'Hello world!',
        'sender' => new SendTransacEmailRequestSender([
            'name' => 'Bob Wilson',
            'email' => 'bob.wilson@example.com',
        ]),
        'to' => [
            new SendTransacEmailRequestToItem([
                'email' => 'sarah.davis@example.com',
                'name' => 'Sarah Davis',
            ]),
        ],
    ])
);

Usage

Instantiate and use the client with the following:

<?php

namespace Example;

use Brevo\Brevo;
use Brevo\TransactionalEmails\Requests\SendTransacEmailRequest;
use Brevo\TransactionalEmails\Types\SendTransacEmailRequestSender;
use Brevo\TransactionalEmails\Types\SendTransacEmailRequestToItem;

$client = new Brevo(
    apiKey: '<value>',
);
$client->transactionalEmails->sendTransacEmail(
    new SendTransacEmailRequest([
        'htmlContent' => '<html><head></head><body><p>Hello,</p>This is my first transactional email sent from Brevo.</p></body></html>',
        'sender' => new SendTransacEmailRequestSender([
            'email' => 'hello@brevo.com',
            'name' => 'Alex from Brevo',
        ]),
        'subject' => 'Hello from Brevo!',
        'to' => [
            new SendTransacEmailRequestToItem([
                'email' => 'johndoe@example.com',
                'name' => 'John Doe',
            ]),
        ],
    ]),
);

Exception Handling

When the API returns a non-success status code (4xx or 5xx response), an exception will be thrown.

use Brevo\Exceptions\BrevoApiException;
use Brevo\Exceptions\BrevoException;

try {
    $response = $client->transactionalEmails->sendTransacEmail(...);
} catch (BrevoApiException $e) {
    echo 'API Exception occurred: ' . $e->getMessage() . "\n";
    echo 'Status Code: ' . $e->getCode() . "\n";
    echo 'Response Body: ' . $e->getBody() . "\n";
    // Optionally, rethrow the exception or handle accordingly.
}

Advanced

Custom Client

This SDK is built to work with any HTTP client that implements the PSR-18 ClientInterface. By default, if no client is provided, the SDK will use php-http/discovery to find an installed HTTP client. However, you can pass your own client that adheres to ClientInterface:

use Brevo\Brevo;

// Pass any PSR-18 compatible HTTP client implementation.
// For example, using Guzzle:
$customClient = new \GuzzleHttp\Client([
    'timeout' => 5.0,
]);

$client = new Brevo(
    apiKey: 'xkeysib-xxx',
    options: ['client' => $customClient],
);

// Or using Symfony HttpClient:
// $customClient = (new \Symfony\Component\HttpClient\Psr18Client())
//     ->withOptions(['timeout' => 5.0]);
//
// $client = new Brevo(
//     apiKey: 'xkeysib-xxx',
//     options: ['client' => $customClient],
// );

Retries

The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).

A request is deemed retryable when any of the following HTTP status codes is returned:

  • 408 (Timeout)
  • 429 (Too Many Requests)
  • 5XX (Internal Server Errors)

Use the maxRetries request option to configure this behavior.

$response = $client->transactionalEmails->sendTransacEmail(
    ...,
    options: [
        'maxRetries' => 0 // Override maxRetries at the request level
    ]
);

Timeouts

The SDK does not configure a default timeout. Unless you set one, the underlying HTTP client's default applies (Guzzle, for example, defaults to no timeout). Set an explicit timeout (in seconds) at the client or request level:

// Client-level
$client = new Brevo('xkeysib-xxx', ['timeout' => 30.0]);

// Request-level (overrides client setting)
$response = $client->transactionalEmails->sendTransacEmail(
    ...,
    options: [
        'timeout' => 3.0
    ]
);

Timeout forwarding is supported for Guzzle and Symfony HttpClient. Other PSR-18 clients ignore the timeout option.

Logging

The SDK does not ship a built-in logger. Because the client option accepts any PSR-18 HTTP client, you can plug in any PSR-3 logger (Monolog, Symfony Logger, Laravel's Log facade) by wrapping that client with logging middleware. This keeps the SDK lean and lets you reuse the logger your application already configures.

use Brevo\Brevo;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\MessageFormatter;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logger = new Logger('brevo');
$logger->pushHandler(new StreamHandler('php://stderr', Logger::DEBUG));

$stack = HandlerStack::create();
$stack->push(Middleware::log(
    $logger,
    new MessageFormatter('{method} {uri} → {code}')
));

$client = new Brevo('xkeysib-xxx', [
    'client' => new Client(['handler' => $stack, 'timeout' => 5.0]),
]);

Logging happens at the HTTP layer, so each SDK call produces one log line per HTTP request — including retries, which the SDK performs internally.

Warning: The api-key header is sent on every request. If your message formatter includes request headers ({req_headers}), redact it before writing to disk or shipping to an external sink.

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

getbrevo/brevo-php 适用场景与选型建议

getbrevo/brevo-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.89M 次下载、GitHub Stars 达 100, 最近一次更新时间为 2023 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「sdk」 「brevo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 getbrevo/brevo-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 getbrevo/brevo-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 getbrevo/brevo-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.89M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 102
  • 点击次数: 23
  • 依赖项目数: 38
  • 推荐数: 6

GitHub 信息

  • Stars: 100
  • Watchers: 1
  • Forks: 49
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-28