web-push-libs / web-push-php

Web Push library for PHP
MIT License
1.71k stars 298 forks source link

PHP error when trying the example code #264

Closed DonaldDuck313 closed 4 years ago

DonaldDuck313 commented 4 years ago

I just copy-pasted the example code on the home page (the one right below the "usage" title) and I get the a PHP error saying

Fatal error: Uncaught Error: Class 'Jose\Component\Core\Util\Ecc\NistCurve' not found in C:\wamp\www\test\webpush\app\vendor\minishlink\web-push\src\Encryption.php:257 Stack trace: #0 C:\wamp\www\test\webpush\app\vendor\minishlink\web-push\src\Encryption.php(248): Minishlink\WebPush\Encryption::createLocalKeyObjectUsingPurePhpMethod() #1 C:\wamp\www\test\webpush\app\vendor\minishlink\web-push\src\Encryption.php(64): Minishlink\WebPush\Encryption::createLocalKeyObject() #2 C:\wamp\www\test\webpush\app\vendor\minishlink\web-push\src\WebPush.php(220): Minishlink\WebPush\Encryption::encrypt('\v\xE5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00...', 'BHvxbhiAwSyFYHJ...', 'obZiV_CsPTCoOys...', 'aesgcm') #3 C:\wamp\www\test\webpush\app\vendor\minishlink\web-push\src\WebPush.php(168): Minishlink\WebPush\WebPush->prepare(Array) #4 C:\wamp\www\test\webpush\app\index.php(58): Minishlink\WebPush\WebPush->flush() #5 {main} thrown in C:\wamp\www\test\webpush\app\vendor\minishlink\web-push\src\Encryption.php on line 257

Here is my code:


require_once("vendor/autoload.php");

$publicKey = "BHvxbhiAwSyFYHJhyjIn57AKd8o2nRvG9HchevJeQyJ0KGgr_yGP4FeeucXAdOOaabG4Q08C5E_3ncAKVpTvx3w";
$privateKey = "obZiV_CsPTCoOys_4ZPisJ7wAYNU9r2Ci95Kvr9t";

use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription;

// array of notifications
$notifications = [
    [
        'subscription' => Subscription::create([
            'endpoint' => 'https://updates.push.services.mozilla.com/push/abc...', // Firefox 43+,
            'publicKey' => $publicKey, // base 64 encoded, should be 88 chars
            'authToken' => $privateKey, // base 64 encoded, should be 24 chars
        ]),
        'payload' => 'hello !',
    ], [
        'subscription' => Subscription::create([
            'endpoint' => 'https://android.googleapis.com/gcm/send/abcdef...', // Chrome
        ]),
        'payload' => null,
    ], [
        'subscription' => Subscription::create([
            'endpoint' => 'https://example.com/other/endpoint/of/another/vendor/abcdef...',
            'publicKey' => $publicKey,
            'authToken' => $privateKey,
            'contentEncoding' => 'aesgcm', // one of PushManager.supportedContentEncodings
        ]),
        'payload' => '{msg:"test"}',
    ], [
          'subscription' => Subscription::create([ // this is the structure for the working draft from october 2018 (https://www.w3.org/TR/2018/WD-push-api-20181026/) 
              "endpoint" => "https://example.com/other/endpoint/of/another/vendor/abcdef...",
              "keys" => [
                  'p256dh' => '(stringOf88Chars)',
                  'auth' => '(stringOf24Chars)'
              ],
          ]),
          'payload' => '{"msg":"Hello World!"}',
      ],
];

$webPush = new WebPush();

// send multiple notifications with payload
foreach ($notifications as $notification) {
    $webPush->sendNotification(
        $notification['subscription'],
        $notification['payload'] // optional (defaults null)
    );
}

/**
 * Check sent results
 * @var MessageSentReport $report
 */
foreach ($webPush->flush() as $report) {
    $endpoint = $report->getRequest()->getUri()->__toString();

    if ($report->isSuccess()) {
        echo "[v] Message sent successfully for subscription {$endpoint}.";
    } else {
        echo "[x] Message failed to sent for subscription {$endpoint}: {$report->getReason()}";
    }
}

/**
 * send one notification and flush directly
 * @var \Generator<MessageSentReport> $sent
 */
$sent = $webPush->sendNotification(
    $notifications[0]['subscription'],
    $notifications[0]['payload'], // optional (defaults null)
    true // optional (defaults false)
);
Minishlink commented 4 years ago

You don't have the required dependencies