3

I used the web push library to send push notifications https://github.com/web-push-libs/web-push-php

I getting an internal error while trying to sen push notification

I checked in both PHP version:7.1.22,7.2.9-1
Apache error log throws :

[:error][client ::1:33302] PHP Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE) in /PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php on line 41, referer: http://localhost/PWA/web-push-php-example/src/

And i also tried in Ngnix / error log :

17:22:36 [error] 20232#20232: *46 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: endpoint in /var/www/html/PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php on line 69 PHP message: PHP Fatal error: Uncaught TypeError: Argument 1 passed to Minishlink\WebPush\Subscription::__construct() must be of the type string, null given, called in /var/www/html/PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php on line 72 and defined in /var/www/html/PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php:39 Stack trace: thrown in /var/www/html/PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php on line 39" while reading response header from upstream, client: 127.0.0.1, server: local.pwa.com, request: "POST /PWA/web-push-php-example/src/send_push_notification.php HTTP/2.0", upstream: "fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "localhost", referrer: "https://localhost/PWA/web-push-php-example/src/"

PHP code :

<?php
require __DIR__ . '/../vendor/autoload.php';
use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription;

// here I'll get the subscription endpoint in the POST parameters
// but in reality, you'll get this information in your database
// because you already stored it (cf. push_subscription.php)
$sub =json_decode(file_get_contents('php://input'), true);
$sub_endpoint =$sub['endpoint'];
$sub_publicKey =$sub['publicKey'];
$sub_authToken =$sub['authToken'];
$sub_contentEncoding =$sub['contentEncoding'];
$notifications = [
    [
        'subscription' => Subscription::create([            
            'endPoint' => $sub_endpoint,
            'publicKey' => $sub_publicKey,            
            'authToken' => $sub_authToken,            
            'contentEncoding' => $sub_contentEncoding, // one of PushManager.supportedContentEncodings
        ]),
        'payload' => '{msg:"test"}',
    ],
];

$auth = array(
    'VAPID' => array(
        'subject' => 'mailto:me@website.com', // can be a mailto: or your website address
        'publicKey' => 'BCmti7ScwxxVAlB7WAyxoOXtV7J8vVCXwEDIFXjKvD-ma-yJx_eHJLdADyyzzTKRGb395bSAtxlh4wuDycO3Ih4', // (recommended) uncompressed public key P-256 encoded in Base64-URL
        'privateKey' => 'HJ*******************' // (recommended) in fact the secret multiplier of the private key encoded in Base64-URL
        //'pemFile' => './keys/private_key.pem' // if you have a PEM file and can link to it on your filesystem        
    ),
);
$defaultOptions = array(
    'TTL' => 300, // defaults to 4 weeks
    'urgency' => 'normal', // protocol defaults to "normal"
    'topic' => 'push', // not defined by default - collapse_key
);

$webPush = new WebPush($auth, $defaultOptions);

// send multiple notifications with payload

$webPush->flush();

// send one notification and flush directly
$webPush->sendNotification(
    $notifications[0]['subscription'],
    $notifications[0]['payload'], // optional (defaults null)
    true // optional (defaults false)
);
Harish Karthick
  • 710
  • 2
  • 12
  • 26
  • Here's the problems https://github.com/web-push-libs/web-push-php/blob/master/src/Subscription.php#L41-L43. I must say I've never seen a `__construct()` with arguments starting with `?` before. What's that about? Incidentally, the Travis build of that package is failing, are you sure it actually works properly? – delboy1978uk Sep 21 '18 at 11:07
  • Does this answer your question? [Web Push Notification: How to use Web Push PHP Library?](https://stackoverflow.com/questions/44474960/web-push-notification-how-to-use-web-push-php-library) – Ikenitenine Mar 08 '23 at 19:30

2 Answers2

1

Are you absolutely sure you are actually running PHP 7.21 or 7.2? The problem is the question marks in the constructor here:

https://github.com/web-push-libs/web-push-php/blob/master/src/Subscription.php#L41-L43

As you can see from this 3v4l code, it works on all versions over 7.1:

<?php
class X
{
    public function __construct(
        string $endpoint,
        ?string $publicKey = null,
        ?string $authToken = null,
        ?string $contentEncoding = null
    ) {
        $this->endpoint = $endpoint;

    }
}

$x = new X('blah', 'blahblah');

https://3v4l.org/A1XeN

All v5 iterations of the code produce your error:

Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE) in /in/A1XeN on line 6 
delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • This my php version : PHP 7.2.9-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Aug 19 2018 07:16:12) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.9-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies – Harish Karthick Sep 21 '18 at 11:32
  • This is my another php version i tried : PHP 7.1.22 (cli) (built: Sep 12 2018 07:22:13) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.1.22, Copyright (c) 1999-2018, by Zend Technologies – Harish Karthick Sep 21 '18 at 11:45
1

@Harish, Constructor '?' should work from PHP version 7.1. I find there is a mistake in your parameter values.

__construct() must be of the type string, null given, called in /var/www/html/PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php

As the error log indicates the endpoint value is passed as null, it should be passed as string value.

The variable your passing in the notification as endPoint but in lib it assigned as endpoint.

Santhosh
  • 142
  • 6