【问题标题】:Web Push php libriary : Throws internal errorWeb Push php 库:引发内部错误
【发布时间】:2019-02-25 17:53:05
【问题描述】:

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

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

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

[:error][client ::1:33302] PHP Parse 错误:语法错误,意外 '?',期望变量 (T_VARIABLE) 在 /PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php 第41行,referer:http://localhost/PWA/web-push-php-example/src/

我也试过 Ngnix / 错误日志:

17:22:36 [错误] 20232#20232: *46 FastCGI 在标准错误中发送:“PHP 消息:PHP 注意:未定义索引:端点在 /var/www/html/PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php 在第 69 行 PHP 消息:PHP 致命错误:未捕获的类型错误:参数 1 传递给 Minishlink\WebPush\Subscription::__construct() 必须是 类型字符串,给定空值,调用 /var/www/html/PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php 在第 72 行并定义在 /var/www/html/PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php:39 堆栈跟踪:抛出 /var/www/html/PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php 在第 39 行”,同时从上游读取响应标头,客户端: 127.0.0.1,服务器:local.pwa.com,请求:“POST /PWA/web-push-php-example/src/send_push_notification.php HTTP/2.0”, 上游:“fastcgi://unix:/run/php/php7.2-fpm.sock:”,主机: “本地主机”,推荐人: "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)
);

【问题讨论】:

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


【解决方案1】:

@Harish,构造函数“?”应该从 PHP 7.1 版开始工作。我发现你的参数值有错误。

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

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

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

【讨论】:

    【解决方案2】:

    您确定您实际运行的是 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 PHP Group Zend Engine v3.2.0,版权所有 (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.9-1+ubuntu16.04.1+deb.sury.org+1,版权所有 (c) 1999 -2018,由 Zend Technologies 提供
    • 这是我尝试的另一个 php 版本:PHP 7.1.22 (cli) (built: Sep 12 2018 07:22:13) (NTS) 版权所有 (c) 1997-2018 The PHP Group Zend Engine v3.1.0,版权所有 (c) 1998-2018 Zend Technologies 和 Zend OPcache v7.1.22,版权所有 (c) 1999-2018,Zend Technologies
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多