getbrevo/brevo-php
Legacy (v1.x) PHP SDK for Brevo API v3, auto-generated from OpenAPI/Swagger. Supports PHP 5.6+ and provides wrappers for Brevo features (email, contacts, campaigns, etc.). Maintained for critical security fixes only; migrate to brevo-php v4.
## Getting Started
### Minimal Setup
1. **Installation**
Update to the latest version via Composer:
```bash
composer require getbrevo/brevo-php:^5.0.1
Include the autoloader (unchanged):
require __DIR__ . '/vendor/autoload.php';
First API Call (Updated)
Configure API key (replace YOUR_API_KEY):
$config = Brevo\Client\Configuration::getDefaultConfiguration()
->setApiKey('api-key', 'YOUR_API_KEY');
$apiInstance = new Brevo\Client\Api\ContactsApi(new GuzzleHttp\Client(), $config);
First Use Case: Fetch Contacts (with Consent Groups)
try {
$result = $apiInstance->getContacts();
// New: Check for consentGroups in contact details
if (isset($result['contacts'][0]['consentGroups'])) {
print_r($result['contacts'][0]['consentGroups']);
}
print_r($result);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
New: Consent Groups Management
$consentGroupsApi = new Brevo\Client\Api\ConsentGroupsApi(new GuzzleHttp\Client(), $config);
try {
// List consent groups
$groups = $consentGroupsApi->getConsentGroups();
print_r($groups);
// Create a new consent group
$newGroup = new \Brevo\Client\Model\ConsentGroup([
'name' => 'Marketing Newsletter',
'description' => 'Consent for marketing emails',
'signupMode' => 'SINGLE_OPTIN'
]);
$createdGroup = $consentGroupsApi->createConsentGroup($newGroup);
print_r($createdGroup);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
List Consent Groups:
$consentGroupsApi = new Brevo\Client\Api\ConsentGroupsApi(new GuzzleHttp\Client(), $config);
$groups = $consentGroupsApi->getConsentGroups();
Create/Update Consent Group:
$group = new \Brevo\Client\Model\ConsentGroup([
'name' => 'Promotions',
'description' => 'Opt-in for promotional offers',
'signupMode' => 'DOUBLE_OPTIN' // SINGLE_OPTIN or DOUBLE_OPTIN
]);
$created = $consentGroupsApi->createConsentGroup($group);
Add Contacts to Consent Group:
$consentGroupsApi->addContactsToConsentGroup($groupId, ['user1@example.com', 'user2@example.com']);
Import Contacts with Consent Groups:
$importData = new \Brevo\Client\Model\ImportContactsRequest([
'contacts' => [
['email' => 'user1@example.com', 'attributes' => ['AGE' => '30']],
['email' => 'user2@example.com', 'attributes' => ['AGE' => '25']]
],
'consentGroupIds' => [123, 456] // Assign to multiple consent groups
]);
$apiInstance->importContacts($importData);
Fetch Contact with Consent Status:
$contact = $apiInstance->getContact('user@example.com');
print_r($contact['consentGroups']); // Array of consent group subscriptions
$walletApi = new Brevo\Client\Api\WalletApi(new GuzzleHttp\Client(), $config);
$installUrl = $walletApi->getPassInstallUrl($passId, $contactId);
// Example URL: https://your-app.com/wallet/install?token=...
Update AppServiceProvider to include new APIs:
public function register()
{
$this->app->singleton('brevo.contacts', function ($app) {
$config = Brevo\Client\Configuration::getDefaultConfiguration()
->setApiKey('api-key', config('services.brevo.api_key'));
return new Brevo\Client\Api\ContactsApi(new GuzzleHttp\Client(), $config);
});
$this->app->singleton('brevo.consent-groups', function ($app) {
$config = Brevo\Client\Configuration::getDefaultConfiguration()
->setApiKey('api-key', config('services.brevo.api_key'));
return new Brevo\Client\Api\ConsentGroupsApi(new GuzzleHttp\Client(), $config);
});
$this->app->singleton('brevo.wallet', function ($app) {
$config = Brevo\Client\Configuration::getDefaultConfiguration()
->setApiKey('api-key', config('services.brevo.api_key'));
return new Brevo\Client\Api\WalletApi(new GuzzleHttp\Client(), $config);
});
}
Extend BrevoFacade to support new methods:
class BrevoFacade extends Facade
{
protected static function getFacadeAccessor() { return 'brevo'; }
public static function consentGroups()
{
return app('brevo.consent-groups');
}
public static function wallet()
{
return app('brevo.wallet');
}
}
Usage:
Brevo::consentGroups()->getConsentGroups();
Brevo::wallet()->getPassInstallUrl($passId, $contactId);
Empty Object Serialization (Fixed in v5.0.1)
{} vs []).{} (e.g., {"attributes": {}}).Consent Groups vs. Lists
consentGroups (new) with lists (existing). They serve different purposes:
Wallet Feature Limitations
contactId (not just email).WalletApi.Consent Group Signup Modes
signupMode must be set to SINGLE_OPTIN or DOUBLE_OPTIN. Defaults may vary.signupMode when creating groups:
$group = new \Brevo\Client\Model\ConsentGroup([
'name' => 'Newsletter',
'signupMode' => 'DOUBLE_OPTIN' // Required
]);
Import Contacts with Consent Groups
consentGroupIds in importContacts is optional but requires:
contactId or email for assignment.$groups = Brevo::consentGroups()->getConsentGroups();
$validGroupIds = array_column($groups->getData(), 'id');
if (!in_array($groupId, $validGroupIds)) {
throw new \InvalidArgumentException("Invalid consent group ID");
}
Deprecation Warning
v5.x is backward compatible, plan to migrate to v6 when released (check Brevo’s roadmap).Consent Group Validation Errors
400 Bad Request when creating/updating consent groups.name is provided (required field).signupMode is SINGLE_OPTIN or DOUBLE_OPTIN.name if using PUT to update.Wallet URL Generation Failures
404 Not Found for getPassInstallUrl.passId exists in Brevo’s Wallet section.How can I help you explore Laravel packages today?