1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Rest
* @subpackage Client
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Client.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/** Zend_Service_Abstract */
require_once 'Zend/Service/Abstract.php';
/** Zend_Rest_Client_Result */
require_once 'Zend/Rest/Client/Result.php';
/** Zend_Uri */
require_once 'Zend/Uri.php';
/**
* @category Zend
* @package Zend_Rest
* @subpackage Client
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Rest_Client extends Zend_Service_Abstract
{
/**
* Data for the query
* @var array
*/
protected $_data = array();
/**
* Zend_Uri of this web service
* @var Zend_Uri_Http
*/
protected $_uri = null;
/**
* Constructor
*
* @param string|Zend_Uri_Http $uri URI for the web service
* @return void
*/
public function __construct($uri = null)
{
if (!empty($uri)) {
$this->setUri($uri);
}
}
/**
* Set the URI to use in the request
*
* @param string|Zend_Uri_Http $uri URI for the web service
* @return Zend_Rest_Client
*/
public function setUri($uri)
{
if ($uri instanceof Zend_Uri_Http) {
$this->_uri = $uri;
} else {
$this->_uri = Zend_Uri::factory($uri);
}
return $this;
}
/**
* Retrieve the current request URI object
*
* @return Zend_Uri_Http
*/
public function getUri()
{
return $this->_uri;
}
/**
* Call a remote REST web service URI and return the Zend_Http_Response object
*
* @param string $path The path to append to the URI
* @throws Zend_Rest_Client_Exception
* @return void
*/
final private function _prepareRest($path)
{
// Get the URI object and configure it
if (!$this->_uri instanceof Zend_Uri_Http) {
require_once 'Zend/Rest/Client/Exception.php';
throw new Zend_Rest_Client_Exception('URI object must be set before performing call');
}
$uri = $this->_uri->getUri();
if ($path[0] != '/' && $uri[strlen($uri)-1] != '/') {
$path = '/' . $path;
}
$this->_uri->setPath($path);
/**
* Get the HTTP client and configure it for the endpoint URI. Do this each time
* because the Zend_Http_Client instance is shared among all Zend_Service_Abstract subclasses.
*/
self::getHttpClient()->resetParameters()->setUri($this->_uri);
}
/**
* Performs an HTTP GET request to the $path.
*
* @param string $path
* @param array $query Array of GET parameters
* @throws Zend_Http_Client_Exception
* @return Zend_Http_Response
*/
final public function restGet($path, array $query = null)
{
$this->_prepareRest($path);
$client = self::getHttpClient();
$client->setParameterGet($query);
return $client->request('GET');
}
/**
* Perform a POST or PUT
*
* Performs a POST or PUT request. Any data provided is set in the HTTP
* client. String data is pushed in as raw POST data; array or object data
* is pushed in as POST parameters.
*
* @param mixed $method
* @param mixed $data
* @return Zend_Http_Response
*/
protected function _performPost($method, $data = null)
{
$client = self::getHttpClient();
if (is_string($data)) {
$client->setRawData($data);
} elseif (is_array($data) || is_object($data)) {
$client->setParameterPost((array) $data);
}
return $client->request($method);
}
/**
* Performs an HTTP POST request to $path.
*
* @param string $path
* @param mixed $data Raw data to send
* @throws Zend_Http_Client_Exception
* @return Zend_Http_Response
*/
final public function restPost($path, $data = null)
{
$this->_prepareRest($path);
return $this->_performPost('POST', $data);
}
/**
* Performs an HTTP PUT request to $path.
*
* @param string $path
* @param mixed $data Raw data to send in request
* @throws Zend_Http_Client_Exception
* @return Zend_Http_Response
*/
final public function restPut($path, $data = null)
{
$this->_prepareRest($path);
return $this->_performPost('PUT', $data);
}
/**
* Performs an HTTP DELETE request to $path.
*
* @param string $path
* @throws Zend_Http_Client_Exception
* @return Zend_Http_Response
*/
final public function restDelete($path)
{
$this->_prepareRest($path);
return self::getHttpClient()->request('DELETE');
}
/**
* Method call overload
*
* Allows calling REST actions as object methods; however, you must
* follow-up by chaining the request with a request to an HTTP request
* method (post, get, delete, put):
* <code>
* $response = $rest->sayHello('Foo', 'Manchu')->get();
* </code>
*
* Or use them together, but in sequential calls:
* <code>
* $rest->sayHello('Foo', 'Manchu');
* $response = $rest->get();
* </code>
*
* @param string $method Method name
* @param array $args Method args
* @return Zend_Rest_Client_Result|Zend_Rest_Client Zend_Rest_Client if using
* a remote method, Zend_Rest_Client_Result if using an HTTP request method
*/
public function __call($method, $args)
{
$methods = array('post', 'get', 'delete', 'put');
if (in_array(strtolower($method), $methods)) {
if (!isset($args[0])) {
$args[0] = $this->_uri->getPath();
}
$this->_data['rest'] = 1;
$data = array_slice($args, 1) + $this->_data;
$response = $this->{'rest' . $method}($args[0], $data);
$this->_data = array();//Initializes for next Rest method.
return new Zend_Rest_Client_Result($response->getBody());
} else {
// More than one arg means it's definitely a Zend_Rest_Server
if (sizeof($args) == 1) {
// Uses first called function name as method name
if (!isset($this->_data['method'])) {
$this->_data['method'] = $method;
$this->_data['arg1'] = $args[0];
}
$this->_data[$method] = $args[0];
} else {
$this->_data['method'] = $method;
if (sizeof($args) > 0) {
foreach ($args as $key => $arg) {
$key = 'arg' . $key;
$this->_data[$key] = $arg;
}
}
}
return $this;
}
}
}
|