balfour / php-omnisend
用于与Omnisend API交互的库
0.0.1-alpha
2020-02-24 11:34 UTC
Requires
- ext-json: *
- guzzlehttp/guzzle: ^6.3
- nesbot/carbon: ^2.0
This package is auto-updated.
Last update: 2024-09-24 22:02:26 UTC
README
用于与Omnisend API交互的库。
此库处于早期发布阶段,并正在等待单元测试。
目录
安装
composer require balfour/php-omnisend
使用方法
请参阅https://api-docs.omnisend.com/v3以获取完整的API文档。
创建客户端
use GuzzleHttp\Client; use Balfour\Omnisend\Omnisend; $client = new Client(); $omnisend = new Omnisend($client, 'your-api-key');
联系人
创建联系人
use Carbon\Carbon; use Balfour\Omnisend\ContactGender; use Balfour\Omnisend\ContactStatus; $response = $omnisend->createContact([ 'email' => 'foo@bar.com', 'firstName' => 'John', 'lastName' => 'Doe', 'tags' => [ 'source: Test', ], 'gender' => ContactGender::MALE, 'phone' => '+27721111111', 'birthdate' => '1987-03-28', 'customProperties' => [ 'group' => 'ADMIN', ], ]); // you can also pass an implementation of ContactInterface instead of an array // of attributes // eg: assuming $contact is a model which implements ContactInterface $response = $omnisend->createContact($contact); // specifying opt-in status & optional params $response = $omnisend->createContact( [ 'email' => 'foo@bar.com', 'firstName' => 'John', 'lastName' => 'Doe', 'tags' => [ 'source: Test', ], 'gender' => ContactGender::MALE, 'phone' => '+27721111111', 'birthdate' => '1987-03-28', 'customProperties' => [ 'group' => 'ADMIN', ], ], ContactStatus::UNSUBSCRIBED, Carbon::now(), // status date true, // send welcome email '127.0.0.1', // opt-in ip address (ipv4 or ipv6) Carbon::now(), // opt-in date & time );
更新联系人
$response = $omnisend->updateContact('5d5be5635c3762bd644e947c', [ 'firstName' => 'John', 'lastName' => 'Doe', ]); // you can also pass an implementation of ContactInterface instead of an array // of attributes // eg: assuming $contact is a model which implements ContactInterface $response = $omnisend->updateContact('5d5be5635c3762bd644e947c', $contact); // update contact using email address as identifier $response = $omnisend->updateContactByEmail('matthew@masterstart.com', [ 'firstName' => 'John', 'lastName' => 'Doe', ]);
检索联系人
$response = $omnisend->getContact('5d5be5635c3762bd644e947c');
列出联系人
use Balfour\Omnisend\ContactStatus; $response = $omnisend->listContacts(); // list contacts from offset with limit $response = $omnisend->listContacts( 50, // starting from offset 50 100 // limit to 100 results ); // list contacts by status $response = $omnisend->listContactsByStatus(ContactStatus::SUBSCRIBED);
订阅联系人
$response = $omnisend->subscribeContact('5d5be5635c3762bd644e947c'); // or via email address $response = $omnisend->subscribeContactByEmail('matthew@masterstart.com');
取消订阅联系人
$response = $omnisend->unsubscribeContact('5d5be5635c3762bd644e947c'); // or via email address $response = $omnisend->unsubscribeContactByEmail('matthew@masterstart.com');
事件
列出事件
$response = $omnisend->listEvents();
检索事件
``php $response = $omnisend->getEvent('5d5cf4d98653ed49cd7f1bd2');
// 通过名称检索事件,如果未找到匹配的事件,则函数将返回 'null' $response = $omnisend->getEventByName('Payment Complete'); ``
触发事件
use Balfour\Omnisend\Event; $omnisend->triggerEvent( '5d5cf4d98653ed49cd7f1bd2', 'matthew@masterstart.com', [ 'payment_method' => 'CREDIT CARD', 'amount' => 'R1200.00', ] ); // you can also pass in an implementation of EventInterface $paymentCompleteEvent = new Event( '5d5cf4d98653ed49cd7f1bd2', [ 'payment_method' => 'CREDIT CARD', 'amount' => 'R1200.00', ] ); $omnisend->triggerEvent($paymentCompleteEvent, 'matthew@masterstart.com');
其他调用
对于没有类函数的其他API调用,您可以直接在客户端上调用以下方法。
// examples: $omnisend->get('products'); $omnisend->get('products', ['sort' => 'createdAt']); $omnisend->post('products', [ 'productId' => '1234', 'title' => 'My Product', // ..... ]); $omnisend->put('products', [ 'productId' => '1234', 'title' => 'My Product', // ..... ]); $omnisend->delete('products/1234');
Laravel集成
此包附带Laravel ServiceProvider和实用类,以便轻松集成到Laravel项目中。
use Balfour\Omnisend\Laravel\Event; use Balfour\Omnisend\Laravel\NamedEvent; use Balfour\Omnisend\Omnisend; // resolving client from ioc container $omnisend = app(Omnisend::class); // triggering an event $paymentCompleteEvent = new Event( '5d5cf4d98653ed49cd7f1bd2', [ 'payment_method' => 'CREDIT CARD', 'amount' => 'R1200.00', ] ); $paymentCompleteEvent->fire(); // queue an event // this will use the configured queue (omnisend by default) $paymentCompleteEvent->enqueue('matthew@masterstart.com'); // the queue name can be overridden $paymentCompleteEvent->enqueue('matthew@masterstart.com', 'my_queue'); // the laravel integration also comes with a NamedEvent class, where can event // can be triggered by name instead of id // the event id is resolved at trigger time from the name, and is cached for subsequent // triggers $paymentCompleteEvent = new NamedEvent( 'Payment Complete', [ 'payment_method' => 'CREDIT CARD', 'amount' => 'R1200.00', ] ); $paymentCompleteEvent->fire();
配置
可以使用php artisan vendor:publish命令发布配置。
以下环境变量受到支持
OMNISEND_ENABLED - 启用或禁用Omnisend集成(默认为false)
OMNISEND_API_KEY - 您的Omnisend API密钥
OMNISEND_QUEUE - 作业将在其上处理的队列(默认为omnisend)
OMNISEND_SEND_WELCOME_EMAIL - 如果为true,则会在创建联系人时发送欢迎邮件(默认为false)
OMNISEND_DEFAULT_CONTACT_STATUS - 联系人创建时的默认状态。(默认为subscribed)
作业处理器
以下作业处理器包括
CreateContact
use Balfour\Omnisend\ContactStatus; use Balfour\Omnisend\Laravel\Jobs\CreateContact; use Carbon\Carbon; // eg: assuming $contact is a model which implements ContactInterface CreateContact::enqueue($contact); // or CreateContact::enqueue( $contact, ContactStatus::SUBSCRIBED, true, // send welcome email '127.0.0.1', // opt-in ip Carbon::now() // opt-in date );
UpdateContact
use Balfour\Omnisend\Laravel\Jobs\UpdateContact; // eg: assuming $contact is a model which implements ContactInterface UpdateContact::enqueue($contact);
TriggerEvent
use Balfour\Omnisend\Laravel\Event; use Balfour\Omnisend\Laravel\Jobs\TriggerEvent; $paymentCompleteEvent = new Event( '5d5cf4d98653ed49cd7f1bd2', [ 'payment_method' => 'CREDIT CARD', 'amount' => 'R1200.00', ] ); TriggerEvent::enqueue($event, 'matthew@masterstart.com');