engagespot/php-sdk

这是Engagespot的PHP库

v1.3.1 2024-03-20 19:54 UTC

This package is auto-updated.

Last update: 2024-09-06 08:42:10 UTC


README

Engagespot PHP SDK允许在PHP应用程序中无缝集成并通过Engagespot API发送通知。Engagespot提供了一个统一的平台,通过应用内消息、Web推送和电子邮件等渠道向用户发送有针对性的通知。

安装

使用Composer安装Engagespot PHP SDK

composer require engagespot/php-sdk

入门

步骤 1:获取API凭证

Engagespot上注册账户并获取您的API密钥和API密钥。

步骤 2:初始化EngagespotClient

将Engagespot PHP SDK包含到您的PHP代码中,并使用您的API密钥、API密钥和签名密钥(如有适用)创建一个EngagespotClient实例

以下是两种类型初始化EngagespotClient的示例

  1. 使用单个参数进行初始化
use Engagespot\EngagespotClient;

$apiKey = 'your-api-key';
$apiSecret = 'your-api-secret';
$signingKey = 'your-signing-key';

// Create an instance of EngagespotClient
$engagespot = new EngagespotClient($apiKey, $apiSecret);

在这个例子中,EngagespotClient使用API密钥和API密钥的单个参数进行初始化。

  1. 使用关联数组进行初始化
use Engagespot\EngagespotClient;

$config = [
    'apiKey' => 'your-api-key',
    'apiSecret' => 'your-api-secret',
    'signingKey' => 'your-signing-key',
    'baseUrl' => 'https://api.engagespot.co/v3'
];

// Create an instance of EngagespotClient
$engagespot = new EngagespotClient($config);

在这个例子中,EngagespotClient使用包含API密钥、API密钥、签名密钥和基础URL的关联数组进行初始化。

请记住将'your-api-key''your-api-secret''your-signing-key'替换为您实际的Engagespot API凭证。基础URL是可选的,如果未提供,则默认为'https://api.engagespot.co/v3'。签名密钥也是可选的,如果未使用则可以省略。

步骤 3:发送通知

发送通知

准备通知数据和使用send方法

旧版负载

$notificationData = [
    'notification' => [
        'title' => 'Sample Title',
        'message' => 'Sample Message',
        'icon' => 'sample-icon',
        'url' => 'https://example.com',
        'templateId' => 1,
    ],
    'override' => [
        'channels' => ['inApp', 'webPush'],
        // other properties you want ot override
    ],
    'recipients' => ['user3@example.com'],
    'category' => 'overrideCategory',
    'data' => [
        // custom data as you needed
        ],
    ],
];

$response = $engagespot->send($notificationData);

// Handle the response as needed
var_dump($response);

首选负载

$notificationData = [
    'notification' => [
        'title' => 'Sample Title',
        'message' => 'Sample Message',
        'icon' => 'sample-icon',
        'url' => 'https://example.com',
        'templateIdentifier' => 'sampleTemplate',
        'category' => 'sampleCategory',
        'data' => [
            // custom data as you need
        ],
    ],
    'sendTo' => [
        'topics' => ['topic1', 'topic2'],
        'recipients' => ['user1@example.com', 'user2@example.com'],
    ],
    'override' => [
        'channels' => ['inApp', 'webPush'],
        // other properties you want to override
    ],
];

发送通知

 $response = $engagespot->send($notificationData);

创建或更新用户

use Engagespot\EngagespotClient;

$identifier = 'johndoe@test.com'; // your unique identifier
$profile = [
    'email' => 'johndoe@test.com',
    'any_key' => 'any_value'
];

$engagespot = new EngagespotClient($apiKey, $apiSecret);
$enagagespot->createOrUpdateUser($identifier, $profile);

您可以在配置文件中添加任何键值对。

生成用户令牌

为用户生成JWT令牌进行认证

注意:请记住,为了生成用户令牌,您必须通过关联数组或

$enagagespot->setSigningKey($signingKey);

因为生成用户令牌需要签名密钥

use Engagespot\EngagespotClient;


$apiKey = 'your-api-key';
$apiSecret = 'your-api-secret';
$signingKey = 'your-signing-key';

// Create an instance of EngagespotClient
$engagespot = new EngagespotClient( [
    'apiKey' => $apiKey,
    'apiSecret' => $apiSecret,
    'signingKey' => $signingKey,
    'baseUrl' => 'https://api.engagespot.co/v3' // optional
]);

OR

// Create an instance of EngagespotClient
$engagespot = new EngagespotClient($apiKey, $apiSecret);
$enagagespot->setSigningKey($signingKey);


// Create JWT token for user
$userIdentifier = 'testuser@example.com';
$token = $engagespot->generateUserToken($userIdentifier);

// Use the generated token as needed
var_dump($token);

创建签名密钥

您可以从Engagespot控制台生成公钥-私钥签名密钥对,并且此私钥应该是生成用户令牌的密钥。

signing_key-71ae93037d6197a7db8a1894c2293079

注意:当您生成签名密钥时,Engagespot将在数据库中仅存储公钥。您应该下载私钥并用于签名您的用户令牌。在此步骤之后,您将无法检索私钥。

其他配置

如果需要,设置其他配置选项

$engagespot->setConfig('additionalConfig', 'value');

您可以通过以下方式在初始化EngagespotClient后设置签名密钥:

$signingKey = 'your-signing-key';
$enagagespot->setSigningKey($signingKey);

异常

在初始化或创建通知过程中,如果缺少或参数无效,SDK将抛出\InvalidArgumentException

注意

将占位符值(如'your-api-key''your-api-secret''your-signing-key')替换为您实际的Engagespot API凭证。

有关Engagespot API参数的详细信息,请参阅Engagespot API文档

请随意探索和贡献SDK!