我正在尝试使用PHP Web推送库发送推送通知。一切工作正常,但突然在发送通知时出现致命错误。
这是我用来发送通知的代码:
<?php
require "/vendor/autoload.php";
use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription;
$auth = array(
'VAPID' => array(
'subject' => 'https://www.example.com/',
'publicKey' => 'public key is here',
'privateKey' => 'private key is here',
),
);
$con = mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo "connected";
}
$selectsql = "SELECT * FROM subscription";
$rowcount = mysqli_num_rows(mysqli_query($con,$selectsql));
$result = $con->query($selectsql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$endpoint = $row["endpoint"];
$publickkey = $row["publick_key"];
$userauthtoken = $row["user_auth_token"];
$title = 'Title is Here';
$body = 'Content is Here';
$icon = 'https://www.example.com/fav.png';
$image = 'https://www.example.com/feature-image.jpg';
$link = 'https://www.example.com/landing';
$jsonarray = '["'.$title.'", "'.$body.'", "'.$icon.'", "'.$image.'", "'.$link.'"]';
$webPush = new WebPush($auth);
$res = $webPush->sendNotification(
$endpoint,
$publickkey,
$userauthtoken,
$jsonarray,
true // I'm getting an error here on line 55
);
}
}
?>
这是我在日志中遇到的错误:
2018/12/02 16:43:22 [error] 3627#0: *55 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught TypeError: Argument 1 passed to Minishlink\WebPush\WebPush::sendNotification() must be an instance of Minishlink\WebPush\Subscription, string given, called in /webpush/send.php on line 55 and defined in /vendor/minishlink/web-push/src/WebPush.php:108
服务器:Nginx-CentOS 7 数据库服务器:MySQL 5.6 PHP:v7.2
我尽力在Web和stackoverflow上找到解决方案,但我认为我在找错地方。请在这里指导我!