php - Web Push php 库 : Throws internal error

标签 php service-worker progressive-web-apps web-push

我使用了网络推送库来发送推送通知 https://github.com/web-push-libs/web-push-php

我在尝试发送推送通知时遇到内部错误

我检查了两个 PHP 版本:7.1.22,7.2.9-1
Apache 错误日志抛出:

[: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/

我也在 Ngnix/错误日志中尝试过:

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 代码:

<?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,构造函数“?”应该从 PHP 7.1 版开始工作。我发现您的参数值有误。

__construct() 必须是字符串类型,给定 null,在/var/www/html/PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription 中调用。 php

由于错误日志表明端点值作为 null 传递,因此它应该作为字符串值传递。

您在通知中作为 endPoint 传递的变量,但在 lib 中它被分配为 endpoint

关于php - Web Push php 库 : Throws internal error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52441721/

相关文章:

Angular PWA 在没有 WorkBox 的情况下添加后台同步

javascript - Service Worker 被删除并且即使在注册时指定根范围后也不会收到推送事件

html - 点击 <a> 元素时如何禁用灰色 mask ?

progressive-web-apps - PWA - 保护部分代码不被用户访问

php - 使用 Guzzle 发出带有 GET 参数的 HTTP 请求

php - 如何在 PHP 中对字符串使用 += 运算符?

ASP.Net Core 服务器中的 Angular PWA - Serviceworker-Cache 不工作

javascript - AJAX 响应为空

php - 如何使用某些 php 函数隐藏或编码 url?

android - Chrome 徽章显示在 'Add To Homescreen' 已安装的 PWA 上(仅安装快捷方式)