nztim / mailchimp
Mailchimp API 包装器,包括 Laravel 5 支持。
v5.4
2024-04-17 19:50 UTC
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- phpunit/phpunit: ^10.0
README
为 Mailchimp API v3 提供的基本抽象,与 Laravel 集成
安装
composer require nztim/mailchimp
- 对于 Laravel 支持
- Laravel 5.5+ 将自动发现此包,对于更早的版本,您需要
- 将服务提供者添加到
config/app.php
:NZTim\Mailchimp\MailchimpServiceProvider::class,
- 注册外观:
'Mailchimp' => NZTim\Mailchimp\MailchimpFacade::class,
- 将服务提供者添加到
- 添加
.env
中的MC_KEY
值(您的 API 密钥) - 可选,发布配置文件
php artisan vendor:publish --provider=NZTim\Mailchimp\MailchimpServiceProvider
- Laravel 5.5+ 将自动发现此包,对于更早的版本,您需要
使用方法
- 在 Laravel 5 中,使用
Mailchimp
外观或通过容器注入NZTim\Mailchimp\Mailchimp
。 - 或者,使用 API 密钥实例化:
$mc = new NZTim\Mailchimp\Mailchimp($apikey)
// Get an array of all available lists: Mailchimp::getLists(); // Get lists with parameters - get IDs of lists a user is subscribed to: Mailchimp::getLists(['email' => 'user@example.com', 'fields' => 'lists.id']); // Check to see if an email address is subscribed to a list: Mailchimp::check($listId, $emailAddress); // Returns boolean // Check the staus of a subscriber: Mailchimp::status($listId, $emailAddress); // Returns 'subscribed', 'unsubscribed', 'cleaned', 'pending', 'transactional' or 'not found' // Get subscriber tags: Mailchimp::getTags($listId, $emailAddress); // Returns array of Tag objects // Add tags to a subscriber Mailchimp::addTags($listId, $emailAddress, $tags); // $tags = ['tag1', 'tag2'] // Remove tags from a subscriber Mailchimp::removeTags($listId, $emailAddress, $tags); // $tags = ['tag1', 'tag2'] // Remove all tags from a subscriber Mailchimp::removeAllTags($listId, $emailAddress); // Adds/updates an existing subscriber: Mailchimp::subscribe($listId, $emailAddress, $merge = [], $confirm = true); // Use $confirm = false to skip double-opt-in if you already have permission. // This method will update an existing subscriber and will not ask an existing subscriber to re-confirm. // Unsubscribe a member (set status to 'unsubscribed'): Mailchimp::unsubscribe($listId, $emailAddress); // Archive a member (no longer counts towards audience limits): Mailchimp::archive($listId, $emailAddress); // Permanently delete a member record: Mailchimp::delete($listId, $emailAddress); // Use with care: deleted members cannot be re-added without the user subscribing via a Mailchimp-hosted form with double-opt-in confirmation. // Directly call the API: Mailchimp::api($method, $endpoint, $data = []); // Returns an array.
要访问 v3 API 中所有可用的成员属性,请使用 Member 类来订阅和更新列表成员
$member = (new NZTim\Mailchimp\Member($email)) ->merge_fields(['FNAME' => 'First name']) ->email_type('text') ->confirm(false); Mailchimp::addUpdateMember($listId, $member);
与 subscribe()
方法一样,双重确认默认为是,但现有成员不会要求重新验证,因此您可以使用相同的方法进行创建和更新,无需检查。
错误
- 对所有错误抛出异常。
- 网络/通信错误通常为
ConnectionException
类型。 - API 错误将是基本类型
NZTim\Mailchimp\MailchimpException
,例如,API 密钥错误,列表不存在。 NZTim\Mailchimp\Exception\MailchimpBadRequestException
包含一个response()
方法,尝试将响应体作为数组提供,以便于自动处理某些错误类型。- 合并字段的问题
- 如果您在添加新订阅者时收到错误,请检查所需的合并字段。
- 合并字段区分大小写。
- 如果需要,可以使用
Mailchimp::addUpdateMemberSkipMergeValidation()
。
示例
// Laravel: // Subscribe a user to your list, existing subscribers will not receive confirmation emails Mailchimp::subscribe('listid', 'user@domain.com'); // Subscribe a user to your list with merge fields and double-opt-in confirmation disabled Mailchimp::subscribe('listid', 'user@domain.com', ['FNAME' => 'First name', 'LNAME' => 'Last name'], false); // Subscribe/update a user using the Member class $member = (new NZTim\Mailchimp\Member($email))->interests(['abc123fed' => true])->language('th'); Mailchimp::addUpdateMember('listid', $member);
升级
- 到 v5.0:需要 PHP 8.1
- 到 v4.0
- 现在需要 PHP 7.4 | 8.0
- 使用取消订阅并存档以最大限度地利用免费账户。
- 到 v3.0
- 现在对所有错误抛出异常,必要时使用 try/catch
- 双重确认现在是默认设置,根据需要更新
Mailchimp::subscribe()