Php Laravel Pusher数组_merge:参数2应为数组,给定null
我正在按照pusher的教程在网站上显示通知。一切都符合教程的要求,但是当我试图访问Php Laravel Pusher数组_merge:参数2应为数组,给定null,php,laravel,websocket,pusher,Php,Laravel,Websocket,Pusher,我正在按照pusher的教程在网站上显示通知。一切都符合教程的要求,但是当我试图访问localhost:8000/test上的通知时,出现了这个特殊的错误,我不知道如何修复它 预期结果:通知发送消息 输出:数组_merge()错误 相关教程: 相关文件:C:\xampp\htdocs\inventory prototype\vendor\pusher\pusher-php-server\src\pusher.php:518 这是我的事件/ItemAdd: class ItemAdd imple
localhost:8000/test
上的通知时,出现了这个特殊的错误,我不知道如何修复它
预期结果:通知发送消息
输出:数组_merge()错误
相关教程:
相关文件:C:\xampp\htdocs\inventory prototype\vendor\pusher\pusher-php-server\src\pusher.php:518
这是我的事件/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!";
});
供应商/pusher/src/pusher.php->Trigger
/**
* 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中得到解决 您可以在以下问题中找到此问题的解决方案:
您可能需要使用composer require pusher/pusher-php服务器^4.1
-此库的v5.0.0支持尚未添加到Laravel中
我看不出$params在错误中初始化的位置message@ibrahim-dogan i添加了一段函数,其中$params是declared,您可以尝试
$all_params=array_merge($post_params,$params???[])
让我知道它是否有效,你希望$params作为一个数组,但你调用触发器的地方可能会将其传递为null(顺便说一句,如果你的PHP版本小于7,它将不起作用)。我确实尝试过,它会抛出apErrorException
,这并没有什么好的,我不认为我应该弄乱这些供应商的东西。虽然这个链接可能回答这个问题,最好在这里包括答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效-