@ 2015-06-23T09:30:44Z <?php
class JsonRpcFault extends Exception {}
class JsonRpcClient {
private $uri;
public function __construct($uri) {
$this->uri = $uri;
}
private function generateId() {
$chars = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));
$id = '';
for($c = 0; $c < 16; ++$c)
$id .= $chars[mt_rand(0, count($chars) - 1)];
return $id;
}
public function __call($name, $arguments) {
$id = $this->generateId();
$request = array(
'jsonrpc' => '2.0',
'method' => $name,
'params' => $arguments,
'id' => $id
);
$jsonRequest = json_encode($request);
$ctx = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/json\r\n',
'content' => $jsonRequest
)
));
$jsonResponse = file_get_contents($this->uri, false, $ctx);
if ($jsonResponse === false)
throw new JsonRpcFault('file_get_contents failed', -32603);
$response = json_decode($jsonResponse);
if ($response === null)
throw new JsonRpcFault('JSON cannot be decoded', -32603);
if ($response->id != $id)
throw new JsonRpcFault('Mismatched JSON-RPC IDs', -32603);
if (property_exists($response, 'error'))
throw new JsonRpcFault($response->error->message, $response->error->code);
else if (property_exists($response, 'result'))
return $response->result;
else
throw new JsonRpcFault('Invalid JSON-RPC response', -32603);
}
}
?>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.0.0 - 5.0.5 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.24 , 7.3.0 - 7.3.12 Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/0Qg0D on line 6
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/0Qg0D on line 6
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/0Qg0D on line 6
Process exited with code 255 . preferences:dark mode live preview
203.35 ms | 401 KiB | 260 Q