Laravel Pusher array_merge: 预期参数 2 为数组,给定为 null
Laravel Pusher array_merge: Expected parameter 2 to be an array, null given
我正在按照 pusher 的教程在网站上显示通知。一切都与教程一致,但是当我尝试访问 localhost:8000/test
上的通知时出现了这个特殊错误,我不知道如何修复它。
the error message
预期结果:通知发送消息
输出:array_merge() 错误
相关教程:https://pusher.com/tutorials/web-notifications-laravel-pusher-channels
相关文件:C:\xampp\htdocs\inventory-prototype\vendor\pusher\pusher-php-server\src\Pusher.php:518
这是我的 Events/ItemAdd
:
class ItemAdd implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($user)
{
$this->user = $user;
$this->message = '{ $user } added an item';
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return ['item-add'];
}
}
这是我的 web.php
:
Route::get('test', function () {
dd(event(new App\Events\ItemAdd('Someone')));
return "Event has been sent!";
});
vendor/pusher/src/Pusher.php -> 触发器
/**
* Trigger an event by providing event name and payload.
* Optionally provide a socket ID to exclude a client (most likely the sender).
*
* @param array|string $channels A channel name or an array of channel names to publish the event on.
* @param string $event
* @param mixed $data Event data
* @param array $params [optional]
* @param bool $already_encoded [optional]
*
* @throws PusherException Throws PusherException if $channels is an array of size 101 or above or $socket_id is invalid
* @throws ApiErrorException Throws ApiErrorException if the Channels HTTP API responds with an error
*
* @return object
*/
public function trigger($channels, $event, $data, $params = array(), $already_encoded = false)
{
if (is_string($channels) === true) {
$channels = array($channels);
}
$this->validate_channels($channels);
if (isset($params['socket_id'])) {
$this->validate_socket_id($params['socket_id']);
}
$has_encrypted_channel = false;
foreach ($channels as $chan) {
if (PusherCrypto::is_encrypted_channel($chan)) {
$has_encrypted_channel = true;
}
}
if ($has_encrypted_channel) {
if (count($channels) > 1) {
// For rationale, see limitations of end-to-end encryption in the README
throw new PusherException('You cannot trigger to multiple channels when using encrypted channels');
} else {
$data_encoded = $this->crypto->encrypt_payload($channels[0], $already_encoded ? $data : json_encode($data));
}
} else {
$data_encoded = $already_encoded ? $data : json_encode($data);
}
$query_params = array();
$path = $this->settings['base_path'].'/events';
// json_encode might return false on failure
if (!$data_encoded) {
$this->log('Failed to perform json_encode on the the provided data: {error}', array(
'error' => print_r($data, true),
), LogLevel::ERROR);
}
$post_params = array();
$post_params['name'] = $event;
$post_params['data'] = $data_encoded;
$post_params['channels'] = array_values($channels);
$all_params = array_merge($post_params, $params);
$post_value = json_encode($all_params);
$query_params['body_md5'] = md5($post_value);
$ch = $this->create_curl($this->channels_url_prefix(), $path, 'POST', $query_params);
$this->log('trigger POST: {post_value}', compact('post_value'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_value);
$response = $this->exec_curl($ch);
if ($response['status'] !== 200) {
throw new ApiErrorException($response['body'], $response['status']);
}
$result = json_decode($response['body']);
if (property_exists($result, 'channels')) {
$result->channels = get_object_vars($result->channels);
}
return $result;
}
任何帮助将不胜感激
我随便说说,我刚刚降级到 pusher 4.1,在 composer.json 上寻找 pusher
并将版本更改为 4.1
以防除我以外的任何人得到相同的版本错误。
此错误已在 pusher-http-php 库 v5.0.1 和 Laravel v8.29.0 中解决。 https://github.com/pusher/pusher-http-php/issues/288
您可以在 comment by ben-pusher to issue error array_merge - laravel 8 - php74 #278:
中找到此问题的解决方案
You may need to use composer require pusher/pusher-php-server ^4.1
- support for v5.0.0 of this library hasn't been added to Laravel yet.
我正在按照 pusher 的教程在网站上显示通知。一切都与教程一致,但是当我尝试访问 localhost:8000/test
上的通知时出现了这个特殊错误,我不知道如何修复它。
the error message
预期结果:通知发送消息
输出:array_merge() 错误
相关教程:https://pusher.com/tutorials/web-notifications-laravel-pusher-channels
相关文件:C:\xampp\htdocs\inventory-prototype\vendor\pusher\pusher-php-server\src\Pusher.php:518
这是我的 Events/ItemAdd
:
class ItemAdd implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($user)
{
$this->user = $user;
$this->message = '{ $user } added an item';
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return ['item-add'];
}
}
这是我的 web.php
:
Route::get('test', function () {
dd(event(new App\Events\ItemAdd('Someone')));
return "Event has been sent!";
});
vendor/pusher/src/Pusher.php -> 触发器
/**
* Trigger an event by providing event name and payload.
* Optionally provide a socket ID to exclude a client (most likely the sender).
*
* @param array|string $channels A channel name or an array of channel names to publish the event on.
* @param string $event
* @param mixed $data Event data
* @param array $params [optional]
* @param bool $already_encoded [optional]
*
* @throws PusherException Throws PusherException if $channels is an array of size 101 or above or $socket_id is invalid
* @throws ApiErrorException Throws ApiErrorException if the Channels HTTP API responds with an error
*
* @return object
*/
public function trigger($channels, $event, $data, $params = array(), $already_encoded = false)
{
if (is_string($channels) === true) {
$channels = array($channels);
}
$this->validate_channels($channels);
if (isset($params['socket_id'])) {
$this->validate_socket_id($params['socket_id']);
}
$has_encrypted_channel = false;
foreach ($channels as $chan) {
if (PusherCrypto::is_encrypted_channel($chan)) {
$has_encrypted_channel = true;
}
}
if ($has_encrypted_channel) {
if (count($channels) > 1) {
// For rationale, see limitations of end-to-end encryption in the README
throw new PusherException('You cannot trigger to multiple channels when using encrypted channels');
} else {
$data_encoded = $this->crypto->encrypt_payload($channels[0], $already_encoded ? $data : json_encode($data));
}
} else {
$data_encoded = $already_encoded ? $data : json_encode($data);
}
$query_params = array();
$path = $this->settings['base_path'].'/events';
// json_encode might return false on failure
if (!$data_encoded) {
$this->log('Failed to perform json_encode on the the provided data: {error}', array(
'error' => print_r($data, true),
), LogLevel::ERROR);
}
$post_params = array();
$post_params['name'] = $event;
$post_params['data'] = $data_encoded;
$post_params['channels'] = array_values($channels);
$all_params = array_merge($post_params, $params);
$post_value = json_encode($all_params);
$query_params['body_md5'] = md5($post_value);
$ch = $this->create_curl($this->channels_url_prefix(), $path, 'POST', $query_params);
$this->log('trigger POST: {post_value}', compact('post_value'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_value);
$response = $this->exec_curl($ch);
if ($response['status'] !== 200) {
throw new ApiErrorException($response['body'], $response['status']);
}
$result = json_decode($response['body']);
if (property_exists($result, 'channels')) {
$result->channels = get_object_vars($result->channels);
}
return $result;
}
任何帮助将不胜感激
我随便说说,我刚刚降级到 pusher 4.1,在 composer.json 上寻找 pusher
并将版本更改为 4.1
以防除我以外的任何人得到相同的版本错误。
此错误已在 pusher-http-php 库 v5.0.1 和 Laravel v8.29.0 中解决。 https://github.com/pusher/pusher-http-php/issues/288
您可以在 comment by ben-pusher to issue error array_merge - laravel 8 - php74 #278:
中找到此问题的解决方案You may need to use
composer require pusher/pusher-php-server ^4.1
- support for v5.0.0 of this library hasn't been added to Laravel yet.