As suggested on Discord, the KQL API can be called from PHP using the Remote
class.
How to pass the query
and select
options?
A request like this doesn’t return the expected pages but only the standard site object:
$request = Remote::request('https://yoursite.com/api/query', [
'body' => [
'query' => "page('notes').children",
'select' => [
'text' => "page.text.kirbytext"
]
],
'headers' => [
'Authorization: Basic ' . base64_encode($user . ':' . $pass),
'Content-Type: application/json'
]
]);
Putting the query
into data
instead of body
has no effect.
To check if my setup would work at all, I can add the query directly to the request-url which successfully returns the requested data:
$request = Remote::request("https://yoursite.com/api/query?query=page('notes').children", [ ... ]
Putting the query in the data array should be the way to go, see source code for a get request.
Thank you for your answer. Indeed, the query needs to go into the data
array. I tried that before without luck, just now I realized that the desired method
needs to be passed additionally:
$request = Remote::request("https://yoursite.com/api/query", [
'method' => 'POST',
'data' => [
'query' => "page('notes').children"
],
'headers' => [
'Authorization: Basic ' . base64_encode($user . ':' . $pass),
'Content-Type: application/json'
]
]);
The select
option also works nicely now:
'data' => [
'query' => "page('notes').children",
'select' => [
"title",
"date" => "page.date.toDate('d.m.Y')"
]
]
I wonder, though, is it expected behaviour that it returns NULL
for boolean value (like mentioned in the docs for javaScript calls)?
"title" => true
returns NULL
Side note: According to the docs, KQL API takes POST requests. Hence Remote::request
doesn’t work with 'method' => 'GET'
.
But why does Remote::get()
return the requested data?
Looking at the source code, the query
route accepts both get and post requests: kql/index.php at main · getkirby/kql · GitHub
Must admit that I didn’t have a closer look at the KQL plugin yet.