Core\WebPush\Subscription::endpoint
Returns the URL of the browser's push service for this subscription, for example https://push.example.com/send/abc123. Each browser has its own URL, so you can use it to find a saved subscription again. For example, you delete a subscription by its endpoint when Core\WebPush\Sent::isGone() is true.
The browser wrote this URL, so endpoint() returns a tainted string. Tainted means that the text came from outside your program and nothing has checked it yet. You can print it and compare it. To put it into an HTML page, use Core\Html::escape. The HTTP client does not accept a tainted URL, so your program does not compile if it fetches the endpoint itself. Core\WebPush::send is the way to send to it.
Good to know: Core\WebPush::subscription throws an error for an endpoint that is not an https: URL, so endpoint() always returns an https: URL.
The examples read the endpoint, delete the subscriptions that are gone, and show the endpoint in an HTML page.
Return value
Returns tainted string. The endpoint URL, such as https://push.example.com/send/abc123.
Examples
Read the endpoint
<?nvs// Shows how to read the push service URL of a subscription that a browser sent.
string $json = '{"endpoint":"https://push.example.com/send/abc123",' . '"keys":{"p256dh":"BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",' . '"auth":"BTBZMqHH6r4Tts7J_aSIgg"}}';Core\WebPush\Subscription $subscription = Core\WebPush::subscription($json);
// `endpoint()` returns the URL from the `endpoint` field of the JSON.// This prints "https://push.example.com/send/abc123".echo $subscription->endpoint(), "\n";
// The URL is the same each time you call `endpoint()`, so you can compare two subscriptions with it.Core\WebPush\Subscription $again = Core\WebPush::subscription($json);echo $subscription->endpoint() == $again->endpoint() ? "same browser\n" : "other browser\n";https://push.example.com/send/abc123same browserDelete a subscription that is gone
<?nvs// Shows how to delete the saved subscriptions that the push service no longer knows.
// These replies replace the push service, so the example runs without a network.Core\Test::answerHttp('https://push.example.com/send/*', 201, {});Core\Test::answerHttp('https://push.example.com/old/*', 410, {});
class Store{ // Returns the JSON a browser sends, with the given endpoint. public static function json(string $endpoint): string { return '{"endpoint":"' . $endpoint . '","keys":{"p256dh":' . '"BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",' . '"auth":"BTBZMqHH6r4Tts7J_aSIgg"}}'; }}
array<string> $saved = [ Store::json('https://push.example.com/send/a1'), Store::json('https://push.example.com/old/b2'), Store::json('https://push.example.com/send/c3'),];
Core\Crypto\KeyPair $vapid = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);bytes $payload = Core\Encoding::encodeText('{"title":"New order"}', Core\Charset::Utf8);
// `isGone()` returns `true` when the push service no longer knows the subscription.// The program then deletes it, and finds it by its endpoint.array<string> $kept = [];foreach ($saved as string $json) { Core\WebPush\Subscription $to = Core\WebPush::subscription($json); Core\WebPush\Sent $sent = Core\WebPush::send($to, $payload, $vapid, 'mailto:admin@example.com'); if ($sent->isGone()) { echo 'delete ', $to->endpoint(), "\n"; } else { $kept[] = $json; }}
// This prints "2 kept".echo Core\Arr::count($kept), " kept\n";delete https://push.example.com/old/b22 keptShow it in a page
<?nvs// Shows how to write the endpoint into an HTML page safely.
// The browser can put characters into the URL that have a meaning in HTML, such as `&`.string $json = '{"endpoint":"https://push.example.com/send/abc?user=7&key=x\'y",' . '"keys":{"p256dh":"BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",' . '"auth":"BTBZMqHH6r4Tts7J_aSIgg"}}';Core\WebPush\Subscription $subscription = Core\WebPush::subscription($json);
// `endpoint()` returns a `tainted string`. Tainted means that the text came from outside// your program and nothing has checked it yet.tainted string $endpoint = $subscription->endpoint();
// `Core\Html::escape` writes `&` and `'` as character references, so the page shows them as text.// This prints "<li>https://push.example.com/send/abc?user=7&key=x'y</li>".echo '<li>', Core\Html::escape($endpoint), "</li>\n";<li>https://push.example.com/send/abc?user=7&key=x'y</li>