Jyxo\Rpc\Json\Client::send PHP Method

send() public method

Sends a request and fetches server's response.
public send ( string $method, array $params ) : mixed
$method string Method name
$params array Method parameters
return mixed
    public function send(string $method, array $params)
    {
        // Start profiling
        $this->profileStart();
        // Generates ID
        $id = md5(uniqid((string) rand(), true));
        try {
            // Prepare JSON-RPC request
            $data = json_encode(['method' => $method, 'params' => $params, 'id' => $id]);
            // Fetch response
            $response = $this->process('application/json', $data);
            // Process response
            $response = json_decode($response, true);
        } catch (\Jyxo\Rpc\Exception $e) {
            // Finish profiling
            $this->profileEnd('JSON', $method, $params, $e->getMessage());
            throw new Exception($e->getMessage(), 0, $e);
        }
        // Finish profiling
        $this->profileEnd('JSON', $method, $params, $response);
        // Error in response
        if (!is_array($response) || !isset($response['id'])) {
            throw new Exception('Invalid response data.');
        }
        if ($id !== $response['id']) {
            throw new Exception('Response ID does not correspond to request ID.');
        }
        if (isset($response['error'])) {
            throw new Exception(preg_replace('~\\s+~', ' ', $response['error']['message']), $response['error']['code']);
        }
        if (!isset($response['result'])) {
            throw new Exception('No response data.');
        }
        return $response['result'];
    }
Client