Client.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_XmlRpc
  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. /**
  23. * For handling the HTTP connection to the XML-RPC service
  24. * @see Zend_Http_Client
  25. */
  26. require_once 'Zend/Http/Client.php';
  27. /**
  28. * Enables object chaining for calling namespaced XML-RPC methods.
  29. * @see Zend_XmlRpc_Client_ServerProxy
  30. */
  31. require_once 'Zend/XmlRpc/Client/ServerProxy.php';
  32. /**
  33. * Introspects remote servers using the XML-RPC de facto system.* methods
  34. * @see Zend_XmlRpc_Client_ServerIntrospection
  35. */
  36. require_once 'Zend/XmlRpc/Client/ServerIntrospection.php';
  37. /**
  38. * Represent a native XML-RPC value, used both in sending parameters
  39. * to methods and as the parameters retrieve from method calls
  40. * @see Zend_XmlRpc_Value
  41. */
  42. require_once 'Zend/XmlRpc/Value.php';
  43. /**
  44. * XML-RPC Request
  45. * @see Zend_XmlRpc_Request
  46. */
  47. require_once 'Zend/XmlRpc/Request.php';
  48. /**
  49. * XML-RPC Response
  50. * @see Zend_XmlRpc_Response
  51. */
  52. require_once 'Zend/XmlRpc/Response.php';
  53. /**
  54. * XML-RPC Fault
  55. * @see Zend_XmlRpc_Fault
  56. */
  57. require_once 'Zend/XmlRpc/Fault.php';
  58. /**
  59. * An XML-RPC client implementation
  60. *
  61. * @category Zend
  62. * @package Zend_XmlRpc
  63. * @subpackage Client
  64. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  65. * @license http://framework.zend.com/license/new-bsd New BSD License
  66. */
  67. class Zend_XmlRpc_Client
  68. {
  69. /**
  70. * Full address of the XML-RPC service
  71. * @var string
  72. * @example http://time.xmlrpc.com/RPC2
  73. */
  74. protected $_serverAddress;
  75. /**
  76. * HTTP Client to use for requests
  77. * @var Zend_Http_Client
  78. */
  79. protected $_httpClient = null;
  80. /**
  81. * Introspection object
  82. * @var Zend_Http_Client_Introspector
  83. */
  84. protected $_introspector = null;
  85. /**
  86. * Request of the last method call
  87. * @var Zend_XmlRpc_Request
  88. */
  89. protected $_lastRequest = null;
  90. /**
  91. * Response received from the last method call
  92. * @var Zend_XmlRpc_Response
  93. */
  94. protected $_lastResponse = null;
  95. /**
  96. * Proxy object for more convenient method calls
  97. * @var array of Zend_XmlRpc_Client_ServerProxy
  98. */
  99. protected $_proxyCache = array();
  100. /**
  101. * Flag for skipping system lookup
  102. * @var bool
  103. */
  104. protected $_skipSystemLookup = false;
  105. /**
  106. * Create a new XML-RPC client to a remote server
  107. *
  108. * @param string $server Full address of the XML-RPC service
  109. * (e.g. http://time.xmlrpc.com/RPC2)
  110. * @param Zend_Http_Client $httpClient HTTP Client to use for requests
  111. * @return void
  112. */
  113. public function __construct($server, Zend_Http_Client $httpClient = null)
  114. {
  115. if ($httpClient === null) {
  116. $this->_httpClient = new Zend_Http_Client();
  117. } else {
  118. $this->_httpClient = $httpClient;
  119. }
  120. $this->_introspector = new Zend_XmlRpc_Client_ServerIntrospection($this);
  121. $this->_serverAddress = $server;
  122. }
  123. /**
  124. * Sets the HTTP client object to use for connecting the XML-RPC server.
  125. *
  126. * @param Zend_Http_Client $httpClient
  127. * @return Zend_Http_Client
  128. */
  129. public function setHttpClient(Zend_Http_Client $httpClient)
  130. {
  131. return $this->_httpClient = $httpClient;
  132. }
  133. /**
  134. * Gets the HTTP client object.
  135. *
  136. * @return Zend_Http_Client
  137. */
  138. public function getHttpClient()
  139. {
  140. return $this->_httpClient;
  141. }
  142. /**
  143. * Sets the object used to introspect remote servers
  144. *
  145. * @param Zend_XmlRpc_Client_ServerIntrospection
  146. * @return Zend_XmlRpc_Client_ServerIntrospection
  147. */
  148. public function setIntrospector(Zend_XmlRpc_Client_ServerIntrospection $introspector)
  149. {
  150. return $this->_introspector = $introspector;
  151. }
  152. /**
  153. * Gets the introspection object.
  154. *
  155. * @return Zend_XmlRpc_Client_ServerIntrospection
  156. */
  157. public function getIntrospector()
  158. {
  159. return $this->_introspector;
  160. }
  161. /**
  162. * The request of the last method call
  163. *
  164. * @return Zend_XmlRpc_Request
  165. */
  166. public function getLastRequest()
  167. {
  168. return $this->_lastRequest;
  169. }
  170. /**
  171. * The response received from the last method call
  172. *
  173. * @return Zend_XmlRpc_Response
  174. */
  175. public function getLastResponse()
  176. {
  177. return $this->_lastResponse;
  178. }
  179. /**
  180. * Returns a proxy object for more convenient method calls
  181. *
  182. * @param $namespace Namespace to proxy or empty string for none
  183. * @return Zend_XmlRpc_Client_ServerProxy
  184. */
  185. public function getProxy($namespace = '')
  186. {
  187. if (empty($this->_proxyCache[$namespace])) {
  188. $proxy = new Zend_XmlRpc_Client_ServerProxy($this, $namespace);
  189. $this->_proxyCache[$namespace] = $proxy;
  190. }
  191. return $this->_proxyCache[$namespace];
  192. }
  193. /**
  194. * Set skip system lookup flag
  195. *
  196. * @param bool $flag
  197. * @return Zend_XmlRpc_Client
  198. */
  199. public function setSkipSystemLookup($flag = true)
  200. {
  201. $this->_skipSystemLookup = (bool) $flag;
  202. return $this;
  203. }
  204. /**
  205. * Skip system lookup when determining if parameter should be array or struct?
  206. *
  207. * @return bool
  208. */
  209. public function skipSystemLookup()
  210. {
  211. return $this->_skipSystemLookup;
  212. }
  213. /**
  214. * Perform an XML-RPC request and return a response.
  215. *
  216. * @param Zend_XmlRpc_Request $request
  217. * @param null|Zend_XmlRpc_Response $response
  218. * @return void
  219. * @throws Zend_XmlRpc_Client_HttpException
  220. */
  221. public function doRequest($request, $response = null)
  222. {
  223. $this->_lastRequest = $request;
  224. iconv_set_encoding('input_encoding', 'UTF-8');
  225. iconv_set_encoding('output_encoding', 'UTF-8');
  226. iconv_set_encoding('internal_encoding', 'UTF-8');
  227. $http = $this->getHttpClient();
  228. if($http->getUri() === null) {
  229. $http->setUri($this->_serverAddress);
  230. }
  231. $http->setHeaders(array(
  232. 'Content-Type: text/xml; charset=utf-8',
  233. 'User-Agent: Zend_XmlRpc_Client',
  234. 'Accept: text/xml',
  235. ));
  236. $xml = $this->_lastRequest->__toString();
  237. $http->setRawData($xml);
  238. $httpResponse = $http->request(Zend_Http_Client::POST);
  239. if (! $httpResponse->isSuccessful()) {
  240. /**
  241. * Exception thrown when an HTTP error occurs
  242. * @see Zend_XmlRpc_Client_HttpException
  243. */
  244. require_once 'Zend/XmlRpc/Client/HttpException.php';
  245. throw new Zend_XmlRpc_Client_HttpException(
  246. $httpResponse->getMessage(),
  247. $httpResponse->getStatus());
  248. }
  249. if ($response === null) {
  250. $response = new Zend_XmlRpc_Response();
  251. }
  252. $this->_lastResponse = $response;
  253. $this->_lastResponse->loadXml($httpResponse->getBody());
  254. }
  255. /**
  256. * Send an XML-RPC request to the service (for a specific method)
  257. *
  258. * @param string $method Name of the method we want to call
  259. * @param array $params Array of parameters for the method
  260. * @return mixed
  261. * @throws Zend_XmlRpc_Client_FaultException
  262. */
  263. public function call($method, $params=array())
  264. {
  265. if (!$this->skipSystemLookup() && ('system.' != substr($method, 0, 7))) {
  266. // Ensure empty array/struct params are cast correctly
  267. // If system.* methods are not available, bypass. (ZF-2978)
  268. $success = true;
  269. try {
  270. $signatures = $this->getIntrospector()->getMethodSignature($method);
  271. } catch (Zend_XmlRpc_Exception $e) {
  272. $success = false;
  273. }
  274. if ($success) {
  275. foreach ($params as $key => $param) {
  276. if (is_array($param) && empty($param)) {
  277. $type = 'array';
  278. foreach ($signatures as $signature) {
  279. if (!is_array($signature)) {
  280. continue;
  281. }
  282. if (array_key_exists($key + 1, $signature)) {
  283. $type = $signature[$key + 1];
  284. $type = (in_array($type, array('array', 'struct'))) ? $type : 'array';
  285. break;
  286. }
  287. }
  288. $params[$key] = array(
  289. 'type' => $type,
  290. 'value' => $param
  291. );
  292. }
  293. }
  294. }
  295. }
  296. $request = new Zend_XmlRpc_Request($method, $params);
  297. $this->doRequest($request);
  298. if ($this->_lastResponse->isFault()) {
  299. $fault = $this->_lastResponse->getFault();
  300. /**
  301. * Exception thrown when an XML-RPC fault is returned
  302. * @see Zend_XmlRpc_Client_FaultException
  303. */
  304. require_once 'Zend/XmlRpc/Client/FaultException.php';
  305. throw new Zend_XmlRpc_Client_FaultException($fault->getMessage(),
  306. $fault->getCode());
  307. }
  308. return $this->_lastResponse->getReturnValue();
  309. }
  310. }