The JSON-RPC request environment is encapsulated in the Zend_Json_Server_Request object. This object allows you to set necessary portions of the JSON-RPC request, including the request ID, parameters, and JSON-RPC specification version. It has the ability to load itself via JSON or a set of options, and can render itself as JSON via the toJson() method.
The request object has the following methods available:
setOptions(array $options): Specify object configuration. $options may contain keys matching any 'set' method: setParams(), setMethod(), setId(), and setVersion().
addParam($value, $key = null): Add a parameter to use with the method call. Parameters can be just the values, or can optionally include the parameter name.
addParams(array $params): Add multiple parameters at once; proxies to addParam()
setParams(array $params): Set all parameters at once; overwrites any existing parameters.
getParam($index): Retrieve a parameter by position or name.
getParams(): Retrieve all parameters at once.
setMethod($name): Set the method to call.
getMethod(): Retrieve the method that will be called.
isMethodError(): Determine whether or not the request is malformed and would result in an error.
setId($name): Set the request identifier (used by the client to match requests to responses).
getId(): Retrieve the request identifier.
setVersion($version): Set the JSON-RPC specification version the request conforms to. May be either '1.0' or '2.0'.
getVersion(): Retrieve the JSON-RPC specification version used by the request.
loadJson($json): Load the request object from a JSON string.
toJson(): Render the request as a JSON string.
An HTTP specific version is available via Zend_Json_Server_Request_Http. This class will retrieve the request via php://input, and allows access to the raw JSON via the getRawJson() method.
27.5.1.3.Zend_Json_Server_Response
The JSON-RPC response payload is encapsulated in the Zend_Json_Server_Response object. This object allows you to set the return value of the request, whether or not the response is an error, the request identifier, the JSON-RPC specification version the response conforms to, and optionally the service map.
The response object has the following methods available:
setResult($value): Set the response result.
getResult(): Retrieve the response result.
setError(Zend_Json_Server_Error $error): Set an error object. If set, this will be used as the response when serializing to JSON.
getError(): Retrieve the error object, if any.
isError(): Whether or not the response is an error response.
setId($name): Set the request identifier (so the client may match the response with the original request).
getId(): Retrieve the request identifier.
setVersion($version): Set the JSON-RPC version the response conforms to.
getVersion(): Retrieve the JSON-RPC version the response conforms to.
toJson(): Serialize the response to JSON. If the response is an error response, serializes the error object.
setServiceMap($serviceMap): Set the service map object for the response.
getServiceMap(): Retrieve the service map object, if any.
An HTTP specific version is available via Zend_Json_Server_Response_Http. This class will send the appropriate HTTP headers as well as serialize the response as JSON.
27.5.1.4.Zend_Json_Server_Error
JSON-RPC has a special format for reporting error conditions. All errors need to provide, minimally, an error message and error code; optionally, they can provide additional data, such as a backtrace.
Error codes are derived from those recommended by the XML-RPC EPI project. Zend_Json_Server appropriately assigns the code based on the error condition. For application exceptions, the code '-32000' is used.
Zend_Json_Server_Error exposes the following methods:
setCode($code): Set the error code; if the code is not in the accepted XML-RPC error code range, -32000 will be assigned.
getCode(): Retrieve the current error code.
setMessage($message): Set the error message.
getMessage(): Retrieve the current error message.
setData($data): Set auxiliary data further qualifying the error, such as a backtrace.
getData(): Retrieve any current auxiliary error data.
toArray(): Cast the error to an array. The array will contain the keys 'code', 'message', and 'data'.
toJson(): Cast the error to a JSON-RPC error representation.
27.5.1.5.Zend_Json_Server_Smd