/library/Zend/Rest/Client.php

https://bitbucket.org/khuongduybui/openfisma · PHP · 258 lines · 100 code · 22 blank · 136 comment · 18 complexity · 946c006d3878795603942fe8fda4cb91 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-2011 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-2011 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. 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. * @throws Zend_Http_Client_Exception
  114. * @return Zend_Http_Response
  115. */
  116. public function restGet($path, array $query = null)
  117. {
  118. $this->_prepareRest($path);
  119. $client = self::getHttpClient();
  120. $client->setParameterGet($query);
  121. return $client->request('GET');
  122. }
  123. /**
  124. * Perform a POST or PUT
  125. *
  126. * Performs a POST or PUT request. Any data provided is set in the HTTP
  127. * client. String data is pushed in as raw POST data; array or object data
  128. * is pushed in as POST parameters.
  129. *
  130. * @param mixed $method
  131. * @param mixed $data
  132. * @return Zend_Http_Response
  133. */
  134. protected function _performPost($method, $data = null)
  135. {
  136. $client = self::getHttpClient();
  137. if (is_string($data)) {
  138. $client->setRawData($data);
  139. } elseif (is_array($data) || is_object($data)) {
  140. $client->setParameterPost((array) $data);
  141. }
  142. return $client->request($method);
  143. }
  144. /**
  145. * Performs an HTTP POST request to $path.
  146. *
  147. * @param string $path
  148. * @param mixed $data Raw data to send
  149. * @throws Zend_Http_Client_Exception
  150. * @return Zend_Http_Response
  151. */
  152. public function restPost($path, $data = null)
  153. {
  154. $this->_prepareRest($path);
  155. return $this->_performPost('POST', $data);
  156. }
  157. /**
  158. * Performs an HTTP PUT request to $path.
  159. *
  160. * @param string $path
  161. * @param mixed $data Raw data to send in request
  162. * @throws Zend_Http_Client_Exception
  163. * @return Zend_Http_Response
  164. */
  165. public function restPut($path, $data = null)
  166. {
  167. $this->_prepareRest($path);
  168. return $this->_performPost('PUT', $data);
  169. }
  170. /**
  171. * Performs an HTTP DELETE request to $path.
  172. *
  173. * @param string $path
  174. * @throws Zend_Http_Client_Exception
  175. * @return Zend_Http_Response
  176. */
  177. public function restDelete($path, $data = null)
  178. {
  179. $this->_prepareRest($path);
  180. return $this->_performPost('DELETE', $data);
  181. }
  182. /**
  183. * Method call overload
  184. *
  185. * Allows calling REST actions as object methods; however, you must
  186. * follow-up by chaining the request with a request to an HTTP request
  187. * method (post, get, delete, put):
  188. * <code>
  189. * $response = $rest->sayHello('Foo', 'Manchu')->get();
  190. * </code>
  191. *
  192. * Or use them together, but in sequential calls:
  193. * <code>
  194. * $rest->sayHello('Foo', 'Manchu');
  195. * $response = $rest->get();
  196. * </code>
  197. *
  198. * @param string $method Method name
  199. * @param array $args Method args
  200. * @return Zend_Rest_Client_Result|Zend_Rest_Client Zend_Rest_Client if using
  201. * a remote method, Zend_Rest_Client_Result if using an HTTP request method
  202. */
  203. public function __call($method, $args)
  204. {
  205. $methods = array('post', 'get', 'delete', 'put');
  206. if (in_array(strtolower($method), $methods)) {
  207. if (!isset($args[0])) {
  208. $args[0] = $this->_uri->getPath();
  209. }
  210. $this->_data['rest'] = 1;
  211. $data = array_slice($args, 1) + $this->_data;
  212. $response = $this->{'rest' . $method}($args[0], $data);
  213. $this->_data = array();//Initializes for next Rest method.
  214. return new Zend_Rest_Client_Result($response->getBody());
  215. } else {
  216. // More than one arg means it's definitely a Zend_Rest_Server
  217. if (sizeof($args) == 1) {
  218. // Uses first called function name as method name
  219. if (!isset($this->_data['method'])) {
  220. $this->_data['method'] = $method;
  221. $this->_data['arg1'] = $args[0];
  222. }
  223. $this->_data[$method] = $args[0];
  224. } else {
  225. $this->_data['method'] = $method;
  226. if (sizeof($args) > 0) {
  227. foreach ($args as $key => $arg) {
  228. $key = 'arg' . $key;
  229. $this->_data[$key] = $arg;
  230. }
  231. }
  232. }
  233. return $this;
  234. }
  235. }
  236. }