我使用Web推送库发送推送通知 https://github.com/web-push-libs/web-push-php
尝试发送通知时出现内部错误
我检查了两个PHP版本:7.1.22,7.2.9-1
Apache错误日志抛出:
[:error] [客户端:: 1:33302] PHP解析错误:语法错误,意外 '?',期望变量(T_VARIABLE)在 /PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php 在第41行,引荐来源:http://localhost/PWA/web-push-php-example/src/
我也在Ngnix /错误日志中尝试过:
17:22:36 [错误] 20232#20232:* 46在stderr中发送的FastCGI:“ PHP 消息:PHP注意:未定义的索引:端点位于 /var/www/html/PWA/web-push-php-example/vendor/minishlink/web-push/src/Subscription.php 在第69行上的PHP消息:PHP致命错误:未捕获的TypeError:参数 传递给Minishlink \ WebPush \ Subscription :: __ construct()的1必须是 类型字符串,给定null,在 /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)
);
答案 0 :(得分: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');
代码的所有v5迭代都会产生错误:
Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE) in /in/A1XeN on line 6
答案 1 :(得分:1)
@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 。