wefra/modules/jsonrpc.php

41 lines
1.1 KiB
PHP

<?php
// Please, ensure that php-curl and php-json are available.
function jsonrpc_request($url, $method, $params){
// Pure JSONRPC request without Odoo consideration.
$curl_session = curl_init();
$jsonrpc_id = random_int(0, 1000000000);
$data = [
"jsonrpc" => "2.0",
"method" => $method,
"params" => $params,
"id" => $jsonrpc_id
];
$body = json_encode($data);
curl_setopt($curl_session, CURLOPT_URL, $url);
curl_setopt($curl_session, CURLOPT_POST, 1);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $body);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_session, CURLOPT_HEADER, false);
curl_setopt($curl_session, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
$response = curl_exec($curl_session);
curl_close($curl_session);
$response_object = json_decode($response, true);
if(isset($response["error"])){
error_log(print_r($response, true));
throw new Exception($response["error"]["message"]);
}
return $response_object;
}