pradosoft /
prado
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @author Robin J. Rogge <[email protected]> |
||
| 5 | * @link https://github.com/pradosoft/prado |
||
| 6 | * @license https://github.com/pradosoft/prado/blob/master/LICENSE |
||
| 7 | * @since 3.2 |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace Prado\Util; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * TJsonRpcClient class |
||
| 14 | * |
||
| 15 | * Note: When using setIsNotification(true), *every* following request is also |
||
| 16 | * considered to be a notification until you use setIsNotification(false). |
||
| 17 | * |
||
| 18 | * Usage: |
||
| 19 | * <pre> |
||
| 20 | * $_rpcClient = new TJsonRpcClient('http://host/server'); |
||
| 21 | * $_result = $_rpcClient->remoteMethod($param, $otherParam); |
||
| 22 | * // or |
||
| 23 | * $_result = TJsonRpcClient::create('http://host/server')->remoteMethod($param, $otherParam); |
||
| 24 | * </pre> |
||
| 25 | * |
||
| 26 | * @author Robin J. Rogge <[email protected]> |
||
| 27 | * @since 3.2 |
||
| 28 | */ |
||
| 29 | class TJsonRpcClient extends TRpcClient |
||
| 30 | { |
||
| 31 | // magics |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param string $method RPC method name |
||
| 35 | * @param array $parameters RPC method parameters |
||
| 36 | * @throws TRpcClientRequestException if the client fails to connect to the server |
||
| 37 | * @throws TRpcClientResponseException if the response represents an RPC fault |
||
| 38 | * @return mixed RPC request result |
||
| 39 | */ |
||
| 40 | public function __call($method, $parameters) |
||
| 41 | { |
||
| 42 | // send request |
||
| 43 | $_response = $this->performRequest($this->getServerUrl(), $this->encodeRequest($method, $parameters), 'application/json'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 44 | |||
| 45 | // skip response handling if the request was just a notification request |
||
| 46 | if ($this->getIsNotification()) { |
||
| 47 | return true; |
||
| 48 | } |
||
| 49 | |||
| 50 | // decode response |
||
| 51 | if (($_response = json_decode($_response, true)) === null) { |
||
| 52 | throw new TRpcClientResponseException('Empty response received'); |
||
| 53 | } |
||
| 54 | |||
| 55 | // handle error response |
||
| 56 | if (null !== $_response['error']) { |
||
| 57 | throw new TRpcClientResponseException($_response['error']); |
||
| 58 | } |
||
| 59 | |||
| 60 | return $_response['result']; |
||
| 61 | } |
||
| 62 | |||
| 63 | // methods |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $method method name |
||
| 67 | * @param array $parameters method parameters |
||
| 68 | */ |
||
| 69 | public function encodeRequest($method, $parameters) |
||
| 70 | { |
||
| 71 | static $_requestId; |
||
| 72 | $_requestId = ($_requestId === null) ? 1 : $_requestId + 1; |
||
| 73 | |||
| 74 | return json_encode([ |
||
| 75 | 'method' => $method, |
||
| 76 | 'params' => $parameters, |
||
| 77 | 'id' => $this->getIsNotification() ? null : $_requestId, |
||
| 78 | ]); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Creates an instance of TJsonRpcClient |
||
| 83 | * @param mixed $type unused |
||
| 84 | * @param string $serverUrl url of the rpc server |
||
| 85 | * @param bool $isNotification whether the requests are considered to be notifications (completely ignoring the response) (default: false) |
||
| 86 | */ |
||
| 87 | public static function create($type, $serverUrl, $isNotification = false) |
||
| 88 | { |
||
| 89 | return new self($serverUrl, $isNotification); |
||
| 90 | } |
||
| 91 | } |
||
| 92 |