knocklabs / knock-php
Knock PHP SDK
Installs: 11 388
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 9
Forks: 0
Open Issues: 1
Requires
- php: ^8.0
- ext-json: *
- league/uri-components: ^7.5.1
- 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: ^2.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
- dev-main
- v0.2.0
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-je-update-deps
- dev-JEverhart383-patch-1
- dev-bc/kno-4035/bulk-add-subs
- dev-jazambuja-kno-3768-phpsdk-add-subscriptions-support
- dev-jazambuja-kno-3536-php-sdk-add-schedules-support
- dev-jazambuja-kno-2634-php-sdk-add-support-for-trigger_data
- dev-md-kno-1566
This package is auto-updated.
Last update: 2025-03-12 18:40:25 UTC
README
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' => 'jhammond@ingen.net', ]);
Sending notifies (triggering workflows)
$client->workflows()->trigger('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');
Test
To run tests, first run composer
in the terminal. Once compiled, you can run phpunit tests/
to run the suite.