packet-grid/php-server-sdk

此包的最新版本(dev-master)没有可用的许可信息。

PacketGrid服务器PHP SDK

dev-master 2020-01-16 23:42 UTC

This package is not auto-updated.

Last update: 2024-09-20 23:18:49 UTC


README

通过Packagist仓库使用Composer安装

设置

当您想使用SDK时,必须在PacketGrid构造函数中传递您的Packet Grid租户ID和Packet Grid API密钥。第三个字段是可选的,如果设置为true,则您将使用生产环境的Packet Grid。如果留空或设置为false,则SDK将默认为您的Packet Grid沙盒环境。

建议您将这些变量存储在一个环境文件中,而不是在源代码中。

/**
  * $tenant_id Your Packet Grid tenant id
  * $api_key Your Packet Grid API key
  * $is_production Use the production 
  */
$packetGrid = new PacketGrid($tenant_id, $api_key, $is_production);

创建客户端令牌

// Instantiate the Packet Grid SDK
$packetGrid = new PacketGrid($tenant_id, $api_key, $is_production);

$tokenRequest = new CreateClientToken();
$tokenRequest->setUserId('yourUniqueUserId');

$response = $packetGrid->tokens->createClientToken($tokenRequest);
$token = $response->getToken();

发送通知

// Instantiate the Packet Grid SDK
$packetGrid = new PacketGrid($tenant_id, $api_key, $is_production);

// Create our notification object
$notification = new SendNotification();

// Set the notification recipients
$recipient = new Recipient();
$recipient->setUsername('username');
$recipient->setEmail('user@gmail.com');
$recipient->setPhone('+61434882753'); // If you have a mobile number like 0434882753, format it as +61434882753 (removing the 0)

$notification->setRecipients([
  $recipient
]);

// Set the notification text
$notification->setTitle('Notification Title');
$notification->setDetail('Notification details...');

// Set the payload, which is optional
$notification->setPayload([
  'anyKey' => 'anyValue'
]);

// Set the desired transport channels
$notification->setTransports([
  TransportEnum::EMAIL,
  TransportEnum::FCM,
  TransportEnum::SMS,
]);

// Dispatch our notification
$packetGrid->notifications->send($notification);