/lib/Zend/Rest/Client.php

https://bitbucket.org/mercysam/zfs · PHP · 253 lines · 104 code · 22 blank · 127 comment · 18 complexity · 3882303f1b86e5c062b08ef06085103f MD5 · raw file

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