Web推送PHP库:抛出内部错误。

3
我使用了Web推送库发送推送通知: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/ 我还在Nginx上尝试过,错误日志如下:
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)
);

这里有一个问题 https://github.com/web-push-libs/web-push-php/blob/master/src/Subscription.php#L41-L43。我必须说,我以前从未见过带有以 ? 开头的参数的 __construct()。这是怎么回事?顺便说一下,那个包的 Travis 构建失败了,你确定它实际上能正常工作吗? - delboy1978uk
这个回答解决了你的问题吗?Web Push通知:如何使用Web Push PHP库? - Ikenitenine
2个回答

1

您确定您实际上正在运行PHP 7.21或7.2吗?问题出在这里构造函数中的问号:

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

正如您从这个3v4l代码中看到的,它适用于所有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

所有v5版本的代码都会产生你的错误:
Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE) in /in/A1XeN on line 6 

这是我的 PHP 版本: PHP 7.2.9-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Aug 19 2018 07:16:12) ( NTS ) 版权所有 (c) 1997-2018 The PHP GroupZend Engine v3.2.0,版权所有 (c) 1998-2018 Zend Technologies 带有 Zend OPcache v7.2.9-1+ubuntu16.04.1+deb.sury.org+1,版权所有 (c) 1999-2018,由 Zend Technologies 提供 - Harish Karthick
这是我尝试的另一个 PHP 版本:PHP 7.1.22(cli)(构建日期:2018年9月12日07:22:13)(NTS) 版权所有(c)1997-2018 PHP Group Zend Engine v3.1.0,版权所有(c)1998-2018 Zend Technologies 带有Zend OPcache v7.1.22,版权所有(c)1999-2018,由Zend Technologies提供 - Harish Karthick

1

@Harish,构造函数“?”应该从PHP版本7.1起运行。我发现您的参数值有误。

__construct()必须是字符串类型,但传递了null,在/var/www/html/PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php中调用

正如错误日志所示,端点值被传递为null,应将其作为字符串值传递。

您在通知中传递的变量为endPoint,但在库中分配为endpoint


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接