Knock PHP library
Documentation
See the documentation for PHP usage examples.
Installation
composer require knocklabs/knock-php php-http/guzzle7-adapter
Configuration
To use the library you must provide a secret API key, provided in the Knock dashboard.
use Knock\KnockSdk\Client;
$client = new Client('sk_12345');
Usage
Identifying users
$client->users()->identify('jhammond', [
'name' => 'John Hammond',
'email' => '[email protected]',
]);
Sending notifies (triggering workflows)
$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',
]);
Retrieving users
$client->users()->get('jhammond');
Deleting users
$client->users()->delete('jhammond');
Preferences
$client->users()->setPreferences('jhammond', [
'channel_types' => [
'email' => true,
'sms' => false,
],
'workflows' => [
'dinosaurs-loose' => [
'email' => false,
'in_app_feed': true,
]
]
]);
Getting and setting channel data
$knock->users()->setChannelData('jhammond', '5a88728a-3ecb-400d-ba6f-9c0956ab252f', [
'tokens' => [
$apnsToken
],
});
$knock->users()->getChannelData('jhammond', '5a88728a-3ecb-400d-ba6f-9c0956ab252f');
Canceling workflows
$client->workflows()->cancel('dinosaurs-loose', [
'cancellation_key' => '21e958bb-2517-40bb-aaaa-d40acc26dac3'
// optionally you can specify recipients here
'recipients' => ['jhammond'],
]);
Signing JWTs
You can use the firebase/php-jwt
package to sign JWTs easily. You will need to generate an environment specific signing key, which you can find in the Knock dashboard.
If you're using a signing token you will need to pass this to your client to perform authentication. You can read more about client-side authentication here.
use Firebase\JWT\JWT;
$privateKey = env('KNOCK_SIGNING_KEY');
$encoded = JWT::encode(['sub' => 'jhammond'], $privateKey, 'RS256');