mailerlite/mailerlite-php
Composer 安装命令:
composer require mailerlite/mailerlite-php
包简介
MailerLite PHP SDK
关键字:
README 文档
README
Table of Contents
- Installation
- Usage
- Optional PSR transport (no BC break)
- E-commerce (Products / Orders / Customers)
- Testing
- License
Installation
Requirements
- PHP 7.4
- An API Key from MailerLite
- PSR-7 and PSR-18 based HTTP adapter
Setup
composer require mailerlite/mailerlite-php
If you get an error saying “Could not find resource using any discovery strategy.” it means that all the discovery strategies have failed. Most likely, your project is missing the message factories and/or a PRS-7 implementation. To resolve this you may run
$ composer require php-http/curl-client guzzlehttp/psr7 php-http/message
Usage
Subscriber
More information on request parameters: https://developers.mailerlite.com/docs/subscribers.html
Create
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $data = [ 'email' => 'subscriber@example.com', ]; $response = $mailerLite->subscribers->create($data);
Read
Single record
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $subscriberId = '123'; $response = $mailerLite->subscribers->find($subscriberId);
All records
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $response = $mailerLite->subscribers->get();
Update
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $subscriberId = '123', $data = [ 'fields' => [ 'name' => 'Example', ], ]; $response = $mailerLite->subscribers->update($subscriberId, $data);
Delete
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $subscriberId = '123'; $response = $mailerLite->subscribers->delete($subscriberId);
Campaign
More information on request parameters: https://developers.mailerlite.com/docs/campaigns.html
Create
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $data = [ 'type' => 'regular', 'name' => 'My new campaign', 'language_id' => 10, 'emails' => [ [ 'subject' => 'My new email', 'from_name' => 'me', 'from' => 'me@example.com', 'content' => 'Hello World!', ] ], 'filter' => [], ]; $response = $mailerLite->campaigns->create($data);
Read
Single record
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $campaignId = '123'; $response = $mailerLite->campaigns->find($campaignId);
All records
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $data = [ 'filter' => ['status' => 'sent'], ]; $response = $mailerLite->campaigns->get($data);
Update
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $campaignId = '123'; $data = [ 'name' => 'Changed campaign name', 'emails' => [ [ 'subject' => 'Changed email subject', 'from_name' => 'Changed from name', 'from' => 'changed@example.com', ] ], ]; $response = $mailerLite->campaigns->update($campaignId, $data);
Delete
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $campaignId = '123'; $response = $mailerLite->campaigns->delete($campaignId);
Schedule
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $campaignId = '123'; $data = [ 'delivery' => 'instant', ]; $response = $mailerLite->campaigns->schedule($campaignId, $data);
Cancel
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $campaignId = '123'; $response = $mailerLite->campaigns->cancel($campaignId);
Subscriber activity of sent campaign
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $campaignId = '123'; $data = [ 'type' => 'opened', ]; $response = $mailerLite->campaigns->getSubscriberActivity($campaignId, $data);
Group API
More information on request parameters: https://developers.mailerlite.com/docs/groups.html
Create
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $data = [ "name" => "New group", ]; $response = $mailerLite->groups->create($data);
Read
Single record
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $groupId = '123'; $response = $mailerLite->groups->find($groupId);
All records
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $response = $mailerLite->groups->get();
Update
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $groupId = '123'; $data = [ "name" => "Updated name", ]; $response = $mailerLite->groups->update($groupId, $data);
Delete
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $groupId = '123'; $response = $mailerLite->groups->delete($groupId);
Get subscribers who belong to a group
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $groupId = '123'; $response = $mailerLite->groups->getSubscribers($groupId);
Assign subscriber to a group
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $groupId = '123'; $subscriberId = '456'; $response = $mailerLite->groups->assignSubscriber($groupId, $subscriberId);
unassign subscriber from a group
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $groupId = '123'; $subscriberId = '456'; $response = $mailerLite->groups->unAssignSubscriber($groupId, $subscriberId);
Segment API
More information on request parameters: https://developers.mailerlite.com/docs/segments.html
Read
All records
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $response = $mailerLite->segments->get();
Update
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $segmentId = '123'; $data = [ "name" => "Updated name", ]; $response = $mailerLite->segments->update($segmentId, $data);
Delete
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $segmentId = '123'; $response = $mailerLite->segments->delete($segmentId);
Get subscribers who belong to a segment
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $segmentId = '123'; $response = $mailerLite->segments->getSubscribers($segmentId);
Field API
More information on request parameters: https://developers.mailerlite.com/docs/fields.html
Create
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $data = [ "name" => "New field", "type" => "text", ]; $response = $mailerLite->fields->create($data);
Read
All records
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $response = $mailerLite->fields->get();
Update
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $fieldId = '123'; $data = [ "name" => "Updated name", ]; $response = $mailerLite->fields->update($fieldId, $data);
Delete
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $fieldId = '123'; $response = $mailerLite->fields->delete($fieldId);
Form API
More information on request parameters: https://developers.mailerlite.com/docs/forms.html
Read
All records
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $response = $mailerLite->forms->get('popup', []);
Single
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $formId = '123'; $response = $mailerLite->forms->find($formId);
Update
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $formId = '123'; $data = [ "name" => "Updated name", ]; $response = $mailerLite->forms->update($formId, $data);
Delete
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $formId = '123'; $response = $mailerLite->forms->delete($formId);
Signed up subscribers
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $formId = '123'; $response = $mailerLite->forms->getSignups($formId);
Automation API
More information on request parameters: https://developers.mailerlite.com/docs/automations.html
Read
All records
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $response = $mailerLite->automations->get([]);
Single
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $automationId = '123'; $response = $mailerLite->automations->find($automationId);
Activity
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $automationId = '123'; $data = [ 'filter' => [ 'status' => 'active', ], ]; $response = $mailerLite->automations->activity($automationId, $data);
Webhook API
More information on request parameters: https://developers.mailerlite.com/docs/webhooks.html
Create
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $data = [ "name" => "Name", "events" => ["subscriber.created"], "url": "https://www.cartwright.info/eligendi-soluta-corporis-in-quod-ullam", ]; $response = $mailerLite->webhooks->create($data);
Read
All records
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $response = $mailerLite->webhooks->get([]);
Single
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $webhookId = '123'; $response = $mailerLite->webhooks->find($webhookId);
Update
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $webhookId = '123'; $data = [ "name" => "Updated name", ]; $response = $mailerLite->webhooks->update($webhookId, $data);
Delete
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $webhookId = '123'; $response = $mailerLite->webhooks->delete($webhookId);
Campaign language API
More information about request parameters on https://developers.mailerlite.com/docs/campaign-languages.html
Read
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $response = $mailerLite->campaignLanguages->get();
Timezone API
More information about request parameters on https://developers.mailerlite.com/docs/timezones.html
Read
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $response = $mailerLite->timezones->get();
Batch API
More information about request parameters on https://developers.mailerlite.com/docs/batching.html
E-commerce (Products / Orders / Customers)
E-commerce endpoints follow:
/api/ecommerce/shops/{shopId}/...
shopId is provided per call (there is no separate /shops API).
Products
$res = $mailerLite->ecommerceProducts->create('YOUR_SHOP_ID', [ 'title' => 'Red T-shirt', 'price' => 19.99, 'currency' => 'USD', ]); $res = $mailerLite->ecommerceProducts->find('YOUR_SHOP_ID', 'PRODUCT_ID'); $res = $mailerLite->ecommerceProducts->update('YOUR_SHOP_ID', 'PRODUCT_ID', [ 'title' => 'New title', ]);
Orders
$res = $mailerLite->ecommerceOrders->create('YOUR_SHOP_ID', [ 'order_id' => 'ORD-1001', ]); $res = $mailerLite->ecommerceOrders->find('YOUR_SHOP_ID', 'ORD-1001'); $res = $mailerLite->ecommerceOrders->update('YOUR_SHOP_ID', 'ORD-1001', [ 'status' => 'paid', ]);
Customers
$res = $mailerLite->ecommerceCustomers->get('YOUR_SHOP_ID', ['limit' => 50]); $res = $mailerLite->ecommerceCustomers->create('YOUR_SHOP_ID', ['email' => 'user@example.com']); $res = $mailerLite->ecommerceCustomers->find('YOUR_SHOP_ID', 'CUSTOMER_ID');
Optional PSR transport (no BC break)
use MailerLite\MailerLite; use MailerLite\Http\Adapters\Psr17FactoryAggregate; use Nyholm\Psr7\Factory\Psr17Factory; use GuzzleHttp\Client as GuzzleHttpClient; $ml = new MailerLite(['api_key' => 'YOUR_API_KEY']); $psr17 = new Psr17Factory(); $factories = new Psr17FactoryAggregate($psr17, $psr17); $httpClient = new GuzzleHttpClient(); $ml->enablePsrTransport($httpClient, $factories, 'YOUR_API_KEY', 'https://connect.mailerlite.com'); $ml->subscribers->create(['email' => 'subscriber@example.com']);
Send
use MailerLite\MailerLite; $mailerLite = new MailerLite(['api_key' => 'key']); $data = [ 'requests' => [ [ 'method' => 'post', 'path' => 'api/subscribers', 'body' => [ 'email' => 'new_subscriber@mail.com' ] ] ] ]; $response = $mailerLite->batches->send($data);
Testing
composer test
Support and Feedback
In case you find any bugs, submit an issue directly here in GitHub.
You are welcome to create SDK for any other programming language.
If you have any troubles using our API or SDK free to contact our support by email info@mailerlite.com
License
mailerlite/mailerlite-php 适用场景与选型建议
mailerlite/mailerlite-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 532.48k 次下载、GitHub Stars 达 32, 最近一次更新时间为 2023 年 03 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「email」 「sdk」 「marketing」 「mailerlite」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mailerlite/mailerlite-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mailerlite/mailerlite-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mailerlite/mailerlite-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Historical accounting for contacts
Extensible library for building notifications and sending them via different delivery channels.
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
Extends Mautic Lead Bundle's Lead List (Segment) functionality.
Sendrill DB
Pulls cost data from media advertising services.
统计信息
- 总下载量: 532.48k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 32
- 点击次数: 31
- 依赖项目数: 6
- 推荐数: 3
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-24