I would like to connect two Kirby instances. A hook in the sender instance sends data to the receiver instance. The receiver then updates the corresponding pages.
My problem is that I cannot decode the data in the receiver instance properly.
The sender uses a hook
to send the data. Data gets encoded to JSON:
'hooks' => [
'page.update:after' => function($newPage, $oldPage) {
$request = Remote::request("https://receiver.test/update", [
'method' => 'POST',
'data' => Json::encode([
'page' => [
'id' => 'test'
]
]),
'headers' => [
'Authorization: Basic ' . base64_encode($user . ':' . $pass),
'Content-Type: application/json'
]
]);
}
]
The receiver instance uses a route
to process the data:
'routes' => [
[
'pattern' => 'test',
'method' => 'POST',
'action' => function () {
$data = kirby()->request()->data();
...
}
]
]
According to the docs, $request->data()
should decode JSON and return an array. But the dump of $data
looks like this:
Array
(
[{"page":{"id":"test"}}] =>
)
Consequently, I can’t access $data['page']
(error: Undefined index: page).
Trying to Json::decode(kirby()->request())
results in the error: Invalid JSON data; please pass a string.
I would rather expect $data
to look like this:
Array
(
"page" => [
"id" => "test"
]
)
How can I properly decode the incoming data?