herald-project/client-php

PHP Herald 客户端

v2.8.1 2023-05-05 12:21 UTC

README

从您的应用程序发送美观的消息。

安装

composer require herald-project/client-php

示例

use Herald\Client\Client as HeraldClient;
use Herald\Client\Message as HeraldMessage;

// get the client with explicit parameters
$herald = new HeraldClient(
    '[herald api username]',
    '[herald api password]',
    '[herald server uri]',
    '[herald transport, e.g. smtp]'),
    '[herald account]',
    '[herald library]',
);

// get the client by DSN

$herald = HeraldClient::fromDsn('https://username:password@herald.dev/myAccount/myLibrary/myTransport');


// check template existance.
if ($herald->templateExists('signup')) {
    // get the message
    $message = new HeraldMessage();
    // use the template
    $message->setTemplate('signup');
    // set to address
    $message->setToAddress($emailAddress, $customerName);
    // populate data
    $message->setData('firstname', 'Hongliang');
    $message->setData('nickname', 'exploder hater');
    // send
    $herald->send($message);
}

CLI 使用

您可以使用 bin/herald-client CLI 应用程序来对您的 herald 服务器运行命令。

应用程序需要一些配置指令才能运行

  • 用户名 + 密码:您用于连接的帐户。可以是用户帐户,也可以是 API 密钥+密钥对
  • apiurl:herald 的基本 URL,(即 https://herald.dev),后缀为 /api/v2
  • 帐户 + 库:这是您用于创建模板、布局和传输的库标识符

您可以通过以下 3 种方式传递此数据

单个选项

例如

./bin/herald-client --username=x --password=y --apiurl=https://herald.dev/api/v2 --account=test --library=test template:exists welcome

单个 DSN

例如

./bin/herald-client --dsn=https://x:y@herald.dev/test/test/mandrill

通过环境变量

您可以通过定义环境变量 HERALD_DSN 来设置一个有效的 URL,这样您就不需要向 CLI 应用程序传递任何选项

通过 .env

在运行任何命令之前,Herald CLI 应用程序会加载 .env 文件,允许您创建如下所示的 .env 文件

HERALD_DSN=https://x:y@herald.dev/test/test/mandrill

这样您也不需要为每个命令传递任何选项

示例命令

# get list of all contact lists
bin/herald-client list:list

# get list of contacts in contact list #1
bin/herald-client list:contacts 1

# get list of segments for list #1
bin/herald-client list:segments 1

# get available list_fields for list #1
bin/herald-client list:fields 1

# delete contact #42
#bin/herald-client contact:delete 42

# get properties of contact #6
bin/herald-client contact:properties 6

# add new contact with address 'new.contact@example.com' to list #1
bin/herald-client contact:add 1 new.contact@example.com

# add new property to contact #36 for list field id #4
bin/herald-client property:add 36 4 "some value"

# send message based on template #7 to all contacts in list #1
bin/herald-client list:send 1 7

# send message based on template #1 to contacts in list #1 that meet the conditions for segment #6
bin/herald-client list:send 1 7 6