3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Json\Server
;
12 use Zend\Http\Client
as HttpClient
;
13 use Zend\Server\Client
as ServerClient
;
15 class Client
implements ServerClient
18 * Full address of the JSON-RPC service.
22 protected $serverAddress;
25 * HTTP Client to use for requests.
29 protected $httpClient;
32 * Request of the last method call.
36 protected $lastRequest;
39 * Response received from the last method call.
43 protected $lastResponse;
53 * Create a new JSON-RPC client to a remote server.
55 * @param string $server Full address of the JSON-RPC service.
56 * @param HttpClient $httpClient HTTP Client to use for requests.
58 public function __construct($server, HttpClient
$httpClient = null)
60 $this->httpClient
= $httpClient ?
: new HttpClient();
61 $this->serverAddress
= $server;
65 * Sets the HTTP client object to use for connecting the JSON-RPC server.
67 * @param HttpClient $httpClient New HTTP client to use.
68 * @return Client Self instance.
70 public function setHttpClient(HttpClient
$httpClient)
72 $this->httpClient
= $httpClient;
77 * Gets the HTTP client object.
79 * @return HttpClient HTTP client.
81 public function getHttpClient()
83 return $this->httpClient
;
87 * The request of the last method call.
89 * @return Request Request instance.
91 public function getLastRequest()
93 return $this->lastRequest
;
97 * The response received from the last method call.
99 * @return Response Response instance.
101 public function getLastResponse()
103 return $this->lastResponse
;
107 * Perform an JSOC-RPC request and return a response.
109 * @param Request $request Request.
110 * @return Response Response.
111 * @throws Exception\HttpException When HTTP communication fails.
113 public function doRequest($request)
115 $this->lastRequest
= $request;
117 $httpRequest = $this->httpClient
->getRequest();
118 if ($httpRequest->getUriString() === null) {
119 $this->httpClient
->setUri($this->serverAddress
);
122 $headers = $httpRequest->getHeaders();
123 $headers->addHeaders(array(
124 'Content-Type' => 'application/json',
125 'Accept' => 'application/json',
128 if (!$headers->get('User-Agent')) {
129 $headers->addHeaderLine('User-Agent', 'Zend_Json_Server_Client');
132 $this->httpClient
->setRawBody($request->__toString());
133 $this->httpClient
->setMethod('POST');
134 $httpResponse = $this->httpClient
->send();
136 if (!$httpResponse->isSuccess()) {
137 throw new Exception\
HttpException(
138 $httpResponse->getReasonPhrase(),
139 $httpResponse->getStatusCode()
143 $response = new Response();
145 $this->lastResponse
= $response;
147 // import all response data form JSON HTTP response
148 $response->loadJson($httpResponse->getBody());
154 * Send an JSON-RPC request to the service (for a specific method).
156 * @param string $method Name of the method we want to call.
157 * @param array $params Array of parameters for the method.
158 * @return mixed Method call results.
159 * @throws Exception\ErrorException When remote call fails.
161 public function call($method, $params = array())
163 $request = $this->createRequest($method, $params);
165 $response = $this->doRequest($request);
167 if ($response->isError()) {
168 $error = $response->getError();
169 throw new Exception\
ErrorException(
170 $error->getMessage(),
175 return $response->getResult();
179 * Create request object.
181 * @param string $method Method to call.
182 * @param array $params List of arguments.
183 * @return Request Created request.
185 protected function createRequest($method, array $params)
187 $request = new Request();
188 $request->setMethod($method)
190 ->setId(++
$this->id
);