From ddaa677dd1c03ecfbfadd05d180fc1ccfa41a1e2 Mon Sep 17 00:00:00 2001 From: Dreytac Date: Mon, 20 Apr 2026 19:33:31 +1000 Subject: [PATCH] Create site method to send a Json request using cURL. --- index.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/index.php b/index.php index 2b63ada..f5343af 100644 --- a/index.php +++ b/index.php @@ -179,6 +179,36 @@ Kirby::plugin( return $links; }, + /** + * Make a Json request to a URL using cURL. + * + * This isn't an entirely generic function and may not work as expected. It was created to service the Kirby Deluge plugin. + * + * If the `$method` is `get`, the request is executed and decoded with no further processing. + * + * @param CurlHandle $curl The instance of the cURL object. + * @param int $reqId A request ID. Usually incremented for each request. + * @param string $method The method to use for the request. + * @param mixed $params If method is not `get`, this is an array of parameters to set in the Json request as `params`. + * + * @return Array of decoded Json data. + */ + "makeRequest" => function ($curl, $reqId, $method, $params = []) { + if ($method != "get") { + $postData = Data::encode(["id" => $reqId, "method" => $method, "params" => $params], "json"); + curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); + } + + $response = curl_exec($curl); + + if ($method == "get") { + return Data::decode($response, "json"); + } else { + list($responseHeader, $responseBody) = explode("\r\n\r\n", $response, 2); + + return Data::decode($responseBody, "json"); + } + }, ], ], info: [