PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/library/Zend/XmlRpc/Client.php

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