Create site method to send a Json request using cURL.
This commit is contained in:
30
index.php
30
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: [
|
||||
|
||||
Reference in New Issue
Block a user