src/Client.php
public function execute($request, $attempt = 1)
    {
        // Check request
        if (\is_array($request)) {
            foreach ($request as $value) {
                if (!$value instanceof JsonRequest) {
                    throw new Exceptions\InvalidRequestException('$request must be array of JsonRequest objects');
                }
            }
        } elseif (!$request instanceof JsonRequest) {
            throw new Exceptions\InvalidRequestException('$request must instance of JsonRequest');
        }

        $address = 1 === \count($this->address) ? $this->address[0] : $this->address[\rand(0, \count($this->address))];
        $isNeedResponse = true;

        if ($this->eventDispatcher) {
            $event = $this->eventDispatcher->dispatch(
                Event\JsonRequestEvent::EVENT,
                new Event\JsonRequestEvent($request, $address)
            );

            if ($event->isPropagationStopped()) {
                return null;
            }
        }

        if (\is_array($request)) {
            $requestHeaders = [];

            foreach ($request as $value) {
                $requestHeaders = \array_merge($requestHeaders, $value->headers()->all());
                $isNeedResponse = empty($value->getId()) ? false : true;
            }
        } else {
            $requestHeaders = $request->headers()->all();
            $isNeedResponse = empty($request->getId()) ? false : true;
        }

        // Default requestHeaders
        $requestHeaders['user-agent'] = \array_key_exists('user-agent', $requestHeaders)
            ? $requestHeaders['user-agent'] : 'JSON-RPC client/'.self::VERSION.'/'.PHP_VERSION;

        $requestHeaders['content-type'] = 'application/json';
        $requestHeaders['accept'] = 'application/json';
        $requestHeaders['cache-control'] = 'no-cache';
        $requestHeaders['connection'] = 'close';

        if (empty($this->address)) {
            throw new Exceptions\ConnectionException('Must be set rpc server address');
        }

        $httpRequest = new HttpRequest(
            'POST',
            \trim($address),
            $requestHeaders,
            \json_encode($request, JSON_UNESCAPED_UNICODE)
        );

        try {
            $httpResponse = $this->httpClient->send($httpRequest);
            $response = $this->parserHttp($httpResponse->getBody()->getContents());
        } catch (\Exception $e) {
            if ($attempt <= $this->options['attempts_on_connection_error']) {
                return $this->execute($request, ++$attempt);
            }
            throw new Exceptions\ConnectionException($e->getMessage());
        }

        if (!$isNeedResponse) {
            return null;
        }

        // Set response headers
        if (\is_array($response)) {
            foreach ($response as $value) {
                $value->headers()->add($httpResponse->getHeaders());
            }
        } else {
            $response->headers()->add($httpResponse->getHeaders());
        }

        // Request <-> Response
        if (\is_array($response)) {
            foreach ($response as $res) {
                foreach ($request as $req) {
                    if ($req->getId() === $res->getId()) {
                        $req->setResponse($res);
                    }
                }
            }
        } else {
            $request->setResponse($response);
        }

        if ($this->eventDispatcher) {
            $this->eventDispatcher->dispatch(
                Event\JsonResponseEvent::EVENT,
                new Event\JsonResponseEvent($response, $address)
            );
        }

        return $response;
    }
public function execute($request, $attempt = 1)
    {
        // Check request
        if (\is_array($request)) {
            foreach ($request as $value) {
                if (!$value instanceof JsonRequest) {
                    throw new Exceptions\InvalidRequestException('$request must be array of JsonRequest objects');
                }
            }
        } elseif (!$request instanceof JsonRequest) {
            throw new Exceptions\InvalidRequestException('$request must instance of JsonRequest');
        }

        $address = 1 === \count($this->address) ? $this->address[0] : $this->address[\rand(0, \count($this->address))];
        $isNeedResponse = true;

        if ($this->eventDispatcher) {
            $event = $this->eventDispatcher->dispatch(
                Event\JsonRequestEvent::EVENT,
                new Event\JsonRequestEvent($request, $address)
            );

            if ($event->isPropagationStopped()) {
                return null;
            }
        }

        if (\is_array($request)) {
            $requestHeaders = [];

            foreach ($request as $value) {
                $requestHeaders = \array_merge($requestHeaders, $value->headers()->all());
                $isNeedResponse = empty($value->getId()) ? false : true;
            }
        } else {
            $requestHeaders = $request->headers()->all();
            $isNeedResponse = empty($request->getId()) ? false : true;
        }

        // Default requestHeaders
        $requestHeaders['user-agent'] = \array_key_exists('user-agent', $requestHeaders)
            ? $requestHeaders['user-agent'] : 'JSON-RPC client/'.self::VERSION.'/'.PHP_VERSION;

        $requestHeaders['content-type'] = 'application/json';
        $requestHeaders['accept'] = 'application/json';
        $requestHeaders['cache-control'] = 'no-cache';
        $requestHeaders['connection'] = 'close';

        if (empty($this->address)) {
            throw new Exceptions\ConnectionException('Must be set rpc server address');
        }

        $httpRequest = new HttpRequest(
            'POST',
            \trim($address),
            $requestHeaders,
            \json_encode($request, JSON_UNESCAPED_UNICODE)
        );

        try {
            $httpResponse = $this->httpClient->send($httpRequest);
            $response = $this->parserHttp($httpResponse->getBody()->getContents());
        } catch (\Exception $e) {
            if ($attempt <= $this->options['attempts_on_connection_error']) {
                return $this->execute($request, ++$attempt);
            }
            throw new Exceptions\ConnectionException($e->getMessage());
        }

        if (!$isNeedResponse) {
            return null;
        }

        // Set response headers
        if (\is_array($response)) {
            foreach ($response as $value) {
                $value->headers()->add($httpResponse->getHeaders());
            }
        } else {
            $response->headers()->add($httpResponse->getHeaders());
        }

        // Request <-> Response
        if (\is_array($response)) {
            foreach ($response as $res) {
                foreach ($request as $req) {
                    if ($req->getId() === $res->getId()) {
                        $req->setResponse($res);
                    }
                }
            }
        } else {
            $request->setResponse($response);
        }

        if ($this->eventDispatcher) {
            $this->eventDispatcher->dispatch(
                Event\JsonResponseEvent::EVENT,
                new Event\JsonResponseEvent($response, $address)
            );
        }

        return $response;
    }
Execute json RPC request or batch requests.

@param JsonRequest|JsonRequest[] $request
@param int                       $attempt

@throws \Exception

@return null|JsonResponse|JsonResponse[]