knocklabs / knock-php
Knock PHP SDK
v0.1.9
2023-09-14 13:24 UTC
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- league/uri-components: ^2.4
- php-http/client-common: ^2.5
- php-http/discovery: ^1.14
- php-http/httplug: ^2.3
- php-http/message-factory: ^1.0
- php-http/multipart-stream-builder: ^1.2
- psr/http-client-implementation: ^1.0
- psr/http-factory-implementation: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- http-interop/http-factory-guzzle: ^1.2
- php-http/mock-client: ^1.5
- phpstan/phpstan: ^1.7
- phpstan/phpstan-phpunit: ^1.1
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-14 15:37:32 UTC
README
文档
查看文档以获取PHP使用示例。
安装
composer require knocklabs/knock-php php-http/guzzle7-adapter
配置
要使用此库,您必须提供一个API密钥,该密钥由Knock仪表板提供。
use Knock\KnockSdk\Client; $client = new Client('sk_12345');
使用
识别用户
$client->users()->identify('jhammond', [ 'name' => 'John Hammond', 'email' => 'jhammond@ingen.net', ]);
发送通知(触发工作流程)
$client->notify('dinosaurs-loose', [ // user id of who performed the action 'actor' => 'dnedry', // list of user ids for who should receive the notification 'recipients' => ['jhammond', 'agrant', 'imalcolm', 'esattler'], // data payload to send through 'data' => [ 'type' => 'trex', 'priority' => 1, ], // an optional identifier for the tenant that the notifications belong to 'tenant' => 'jurassic-park', // an optional key to provide to cancel a notify 'cancellation_key' => '21e958bb-2517-40bb-aaaa-d40acc26dac3', ]);
检索用户
$client->users()->get('jhammond');
删除用户
$client->users()->delete('jhammond');
偏好设置
$client->users()->setPreferences('jhammond', [ 'channel_types' => [ 'email' => true, 'sms' => false, ], 'workflows' => [ 'dinosaurs-loose' => [ 'email' => false, 'in_app_feed': true, ] ] ]);
获取和设置通道数据
$knock->users()->setChannelData('jhammond', '5a88728a-3ecb-400d-ba6f-9c0956ab252f', [ 'tokens' => [ $apnsToken ], }); $knock->users()->getChannelData('jhammond', '5a88728a-3ecb-400d-ba6f-9c0956ab252f');
取消工作流程
$client->workflows()->cancel('dinosaurs-loose', [ 'cancellation_key' => '21e958bb-2517-40bb-aaaa-d40acc26dac3' // optionally you can specify recipients here 'recipients' => ['jhammond'], ]);
签名JWT
您可以使用 firebase/php-jwt
包轻松地签名JWT。您需要生成一个特定环境的签名密钥,您可以在Knock仪表板中找到该密钥。
如果您正在使用签名令牌,您需要将其传递给客户端以执行身份验证。您可以在这里了解更多关于客户端身份验证的信息。
use Firebase\JWT\JWT; $privateKey = env('KNOCK_SIGNING_KEY'); $encoded = JWT::encode(['sub' => 'jhammond'], $privateKey, 'RS256');
测试
要运行测试,首先在终端中运行composer
。编译完成后,您可以通过运行phpunit tests/
来运行测试套件。