Client.php 7.2 KB

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