7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
23
/** Zend_Service_Abstract */
24
require_once 'Zend/Service/Abstract.php';
26
/** Zend_Rest_Client_Result */
27
require_once 'Zend/Rest/Client/Result.php';
30
require_once 'Zend/Uri.php';
36
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
37
* @license http://framework.zend.com/license/new-bsd New BSD License
39
class Zend_Rest_Client extends Zend_Service_Abstract
45
protected $_data = array();
48
* Zend_Uri of this web service
51
protected $_uri = null;
56
* @param string|Zend_Uri_Http $uri URI for the web service
59
public function __construct($uri = null)
67
* Set the URI to use in the request
69
* @param string|Zend_Uri_Http $uri URI for the web service
70
* @return Zend_Rest_Client
72
public function setUri($uri)
74
if ($uri instanceof Zend_Uri_Http) {
77
$this->_uri = Zend_Uri::factory($uri);
84
* Retrieve the current request URI object
86
* @return Zend_Uri_Http
88
public function getUri()
94
* Call a remote REST web service URI and return the Zend_Http_Response object
96
* @param string $path The path to append to the URI
97
* @throws Zend_Rest_Client_Exception
100
final private function _prepareRest($path)
102
// Get the URI object and configure it
103
if (!$this->_uri instanceof Zend_Uri_Http) {
104
require_once 'Zend/Rest/Client/Exception.php';
105
throw new Zend_Rest_Client_Exception('URI object must be set before performing call');
108
$uri = $this->_uri->getUri();
110
if ($path[0] != '/' && $uri[strlen($uri)-1] != '/') {
114
$this->_uri->setPath($path);
117
* Get the HTTP client and configure it for the endpoint URI. Do this each time
118
* because the Zend_Http_Client instance is shared among all Zend_Service_Abstract subclasses.
120
self::getHttpClient()->resetParameters()->setUri($this->_uri);
124
* Performs an HTTP GET request to the $path.
126
* @param string $path
127
* @param array $query Array of GET parameters
128
* @return Zend_Http_Response
130
final public function restGet($path, array $query = null)
132
$this->_prepareRest($path);
133
$client = self::getHttpClient();
134
$client->setParameterGet($query);
135
return $client->request('GET');
139
* Perform a POST or PUT
141
* Performs a POST or PUT request. Any data provided is set in the HTTP
142
* client. String data is pushed in as raw POST data; array or object data
143
* is pushed in as POST parameters.
145
* @param mixed $method
147
* @return Zend_Http_Response
149
protected function _performPost($method, $data = null)
151
$client = self::getHttpClient();
152
if (is_string($data)) {
153
$client->setRawData($data);
154
} elseif (is_array($data) || is_object($data)) {
155
$client->setParameterPost((array) $data);
157
return $client->request($method);
161
* Performs an HTTP POST request to $path.
163
* @param string $path
164
* @param mixed $data Raw data to send
165
* @return Zend_Http_Response
167
final public function restPost($path, $data = null)
169
$this->_prepareRest($path);
170
return $this->_performPost('POST', $data);
174
* Performs an HTTP PUT request to $path.
176
* @param string $path
177
* @param mixed $data Raw data to send in request
178
* @return Zend_Http_Response
180
final public function restPut($path, $data = null)
182
$this->_prepareRest($path);
183
return $this->_performPost('PUT', $data);
187
* Performs an HTTP DELETE request to $path.
189
* @param string $path
190
* @return Zend_Http_Response
192
final public function restDelete($path)
194
$this->_prepareRest($path);
195
return self::getHttpClient()->request('DELETE');
199
* Method call overload
201
* Allows calling REST actions as object methods; however, you must
202
* follow-up by chaining the request with a request to an HTTP request
203
* method (post, get, delete, put):
205
* $response = $rest->sayHello('Foo', 'Manchu')->get();
208
* You can also use an HTTP request method as a calling method, using the
209
* path as the first argument:
211
* $rest->get('/sayHello', 'Foo', 'Manchu');
214
* Or use them together, but in sequential calls:
216
* $rest->sayHello('Foo', 'Manchu');
217
* $response = $rest->get();
220
* @param string $method Method name
221
* @param array $args Method args
222
* @return Zend_Rest_Client_Result|Zend_Rest_Client Zend_Rest_Client if using
223
* a remote method, Zend_Rest_Client_Result if using an HTTP request method
225
public function __call($method, $args)
227
$methods = array('post', 'get', 'delete', 'put');
229
if (in_array(strtolower($method), $methods)) {
230
if (!isset($args[0])) {
231
$args[0] = $this->_uri->getPath();
233
$this->_data['rest'] = 1;
234
$data = array_slice($args, 1) + $this->_data;
235
$response = $this->{'rest' . $method}($args[0], $data);
236
return new Zend_Rest_Client_Result($response->getBody());
238
// More than one arg means it's definitely a Zend_Rest_Server
239
if (sizeof($args) == 1) {
240
$this->_data[$method] = $args[0];
241
$this->_data['arg1'] = $args[0];
243
$this->_data['method'] = $method;
244
if (sizeof($args) > 0) {
245
foreach ($args as $key => $arg) {
247
$this->_data[$key] = $arg;