web-push-libs / web-push-php

Web Push library for PHP
MIT License
1.69k stars 295 forks source link

Uncaught Error: Class 'minishlink\WebPush\WebPush' not found #323

Closed LaurentWi closed 6 months ago

LaurentWi commented 3 years ago

Setup

PHP 7.2 Last version of webpush

Please check that you have installed and enabled these PHP extensions :

gmp, mbstring, curl, openssl are all enabled

Please select any browsers that you are experiencing problems with:

All browsers

Please specify the versions (i.e. Chrome Beta, Firefox Beta etc).

All versions

I get the following error message: Fatal error: Uncaught Error: Class 'Minishlink\WebPush\Subscription' not found in ... when using this code: use Minishlink\WebPush\WebPush; use Minishlink\WebPush\Subscription;

// array of notifications $notifications = [ [ 'subscription' => Subscription::create([ 'endpoint' => $endpoint, // Firefox 43+, 'publicKey' => $publicKey, // base 64 encoded, should be 88 chars 'authToken' => $authToken, // base 64 encoded, should be 24 chars ]), 'payload' => 'hello !', ] ];

I have installed the package with composer and i see all files in the directory: Encryption.php MessageSentReport.php Notification.php Subscription.php SubscriptionInterface.php Utils.php VAPID.php WebPush.php

I have seen someone with the same error message in the list of issues here but in his case there was a file missing which is not my case.

Any idea?

Thanks

a-polounovsky commented 2 years ago

Hi

Same problem for me with 6.0 version

You have to make an array of the $notifications an use this method to send :

foreach ($notifications as $notification) { $webPush->queueNotification( $notification['subscription'], $notification['payload'] ); }

foreach ($webPush->flush() as $report) { $endpoint = $report->getRequest()->getUri()->__toString(); if ($report->isSuccess()) {
// do something } else {
//$report->getReason() } }

And don't forget to autoload

yoan-theophile commented 1 year ago

Hi,

I spent over 12 hours looking for solutions ☹ This is how i solve this issue, maybe it is not the best way but it worked for me :

  1. delete composer.lock file
  2. delete vendor folder
  3. remove all the packages from composer.json except minishlink/web-push-php
  4. run composer install
  5. add the other package in composer.json
  6. run composer update

This idea came to me after testing web-push-php in a new project composer and seeing that it worked fine. Hope it will help someone 😊