public static function fromArray(array $json) { $request = new MRPCJsonRequest(); $request->setId($json["id"]); $request->setId($json["method"]); $request->setId($json["params"]); return $request; }
/** * Reads the request and run the web method. */ public function execute($className) { $this->className = $className; // Parse the request $rawRequest = file_get_contents('php://input'); /* @var $request array */ $request = json_decode($rawRequest, true); // Is valid request? if ($request == false) { throw new MRPCJsonServerException(sprintf('Invalid body (%s).', $rawRequest)); } // Does the request respect the 2.0 specification? if ($request["jsonrpc"] != '2.0') { throw new MRPCJsonServerException(sprintf('The request does not respect the 2.0 specification.')); } // Set the request properties $this->request = new MRPCJsonRequest(); $this->request->setMethod($request["method"])->setParams($request["params"])->setId($request["id"]); // Call the procedure/member $callResponse = call_user_func(array($this, $this->request->getMethod()), $this->request->getParams()); // Does the call fail? if ($callResponse === false) { throw new MRPCJsonServerException('No service.'); } }
public function call(MRPCJsonRequest $request) { $this->request = $request; $this->request->setId($this->generateId()); $jsonRequest = json_encode($this->request->toArray()); $ctx = stream_context_create(array('http' => array('method' => 'POST', 'header' => 'Content-Type: application/json\\r\\n', 'content' => $jsonRequest))); $jsonResponse = file_get_contents($this->url, false, $ctx); if ($jsonResponse === false) { throw new MRPCJsonClientException('file_get_contents failed', -32603); } $response = json_decode($jsonResponse); if ($response === null) { throw new MRPCJsonClientException('JSON cannot be decoded', -32603); } if ($response->id != $request->getId()) { throw new MRPCJsonClientException('Mismatched JSON-RPC IDs', -32603); } if (property_exists($response, 'result') === false) { throw new MRPCJsonClientException('Invalid JSON-RPC response', -32603); } $this->response = new MRPCJsonResponse(); $this->response->fromArray($response); }