Core\WebPush::subscription
Core\WebPush::subscription(string $json): Core\WebPush\SubscriptionCore\WebPush::subscription( string $json, ): Core\WebPush\Subscription
Reads the push subscription that a browser sends to your server. In the browser, PushSubscription.toJSON() returns this JSON text. It has an endpoint URL and two keys, keys.p256dh and keys.auth. The result is a Core\WebPush\Subscription, which you pass to Core\WebPush::send.
subscription checks every value once. The endpoint must be an https: URL. p256dh must be a public key on the P-256 curve, and auth must be 16 bytes. The text must be JSON and at most 8192 bytes. If a check fails, subscription throws a RuntimeError, and its message says which check failed. Other fields, such as expirationTime, are ignored.
The browser wrote the endpoint, so endpoint() returns a tainted string.
Save the JSON text with the user, for example in a database column. Before you send a message, call subscription on the saved text again.
The examples read a browser's JSON, catch the error for a text that is not valid, and save a subscription with the user in a route.
Parameters
$jsonstringThe JSON text the browser sent, such as{"endpoint": "https://…", "keys": {"p256dh": "…", "auth": "…"}}. Other fields, such asexpirationTime, are ignored.
Return value
Returns Core\WebPush\Subscription. The subscription, ready to send a message to.
Errors
RuntimeErrorThe text is not JSON or is longer than 8 KiB. Or the endpoint is not anhttps:URL,p256dhis not a P-256 public key, orauthis not 16 bytes. The message says which.
Examples
Read the browser json
<?nvs// Shows how to read the subscription that a browser sends, and print its endpoint.
// This is the JSON that `PushSubscription.toJSON()` returns in the browser.string $json = '{"endpoint":"https://push.example.com/send/abc123",' . '"expirationTime":null,' . '"keys":{"p256dh":"BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",' . '"auth":"BTBZMqHH6r4Tts7J_aSIgg"}}';
// `Core\WebPush::subscription` checks the endpoint and both keys.// The `expirationTime` field is ignored.Core\WebPush\Subscription $subscription = Core\WebPush::subscription($json);
// This prints "https://push.example.com/send/abc123".echo $subscription->endpoint(), "\n";https://push.example.com/send/abc123A subscription that is not valid
<?nvs// Shows how to catch the error for a subscription that a browser did not write.
string $point = 'BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4';
// The first text uses `http:`. The second has an `auth` that is too short.// The third is not JSON.array<string> $texts = [ '{"endpoint":"http://push.example.com/send/abc123","keys":{"p256dh":"' . $point . '","auth":"BTBZMqHH6r4Tts7J_aSIgg"}}', '{"endpoint":"https://push.example.com/send/abc123","keys":{"p256dh":"' . $point . '","auth":"BTBZ"}}', 'not json',];
// Each text throws a `RuntimeError`. Its message says which check failed.foreach ($texts as string $text) { try { Core\WebPush::subscription($text); echo "accepted\n"; } catch (RuntimeError $e) { echo $e->message, "\n"; }}Core\WebPush::subscription(): the endpoint is not an `https:` URL.Core\WebPush::subscription(): `auth` is not 16 bytes.Core\WebPush::subscription(): the subscription is not JSON.Save it with the user
<?nvs// Shows a route that saves a browser's subscription with the user and reads it again later.
class User{ // The JSON text from the browser. It is saved as text, as a database column would save it. public ?tainted string $pushSubscription = null;
public function constructor(public string $email) {}}
class PushRoute{ // Answers `POST /push/subscribe`. The subscription is checked before it is saved. public static function subscribe(User $user, tainted string $body): string { try { Core\WebPush::subscription($body); } catch (RuntimeError $e) { return 'This is not a push subscription.'; } $user->pushSubscription = $body; return 'Saved.'; }}
User $user = new User('ada@example.com');
// The request body is the JSON that the browser sent.echo PushRoute::subscribe($user, Core\Request::body()), "\n";
// Later, the program reads the saved text again before it sends a message.// A real program then passes `$subscription` to `Core\WebPush::send`.?tainted string $saved = $user->pushSubscription;if ($saved != null) { Core\WebPush\Subscription $subscription = Core\WebPush::subscription($saved); echo 'send to ', $subscription->endpoint(), "\n";}Saved.send to https://push.example.com/send/abc123