1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_XmlRpc
* @subpackage Client
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Client.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* For handling the HTTP connection to the XML-RPC service
* @see Zend_Http_Client
*/
require_once 'Zend/Http/Client.php';
/**
* Enables object chaining for calling namespaced XML-RPC methods.
* @see Zend_XmlRpc_Client_ServerProxy
*/
require_once 'Zend/XmlRpc/Client/ServerProxy.php';
/**
* Introspects remote servers using the XML-RPC de facto system.* methods
* @see Zend_XmlRpc_Client_ServerIntrospection
*/
require_once 'Zend/XmlRpc/Client/ServerIntrospection.php';
/**
* Represent a native XML-RPC value, used both in sending parameters
* to methods and as the parameters retrieve from method calls
* @see Zend_XmlRpc_Value
*/
require_once 'Zend/XmlRpc/Value.php';
/**
* XML-RPC Request
* @see Zend_XmlRpc_Request
*/
require_once 'Zend/XmlRpc/Request.php';
/**
* XML-RPC Response
* @see Zend_XmlRpc_Response
*/
require_once 'Zend/XmlRpc/Response.php';
/**
* XML-RPC Fault
* @see Zend_XmlRpc_Fault
*/
require_once 'Zend/XmlRpc/Fault.php';
/**
* An XML-RPC client implementation
*
* @category Zend
* @package Zend_XmlRpc
* @subpackage Client
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_XmlRpc_Client
{
/**
* Full address of the XML-RPC service
* @var string
* @example http://time.xmlrpc.com/RPC2
*/
protected $_serverAddress;
/**
* HTTP Client to use for requests
* @var Zend_Http_Client
*/
protected $_httpClient = null;
/**
* Introspection object
* @var Zend_Http_Client_Introspector
*/
protected $_introspector = null;
/**
* Request of the last method call
* @var Zend_XmlRpc_Request
*/
protected $_lastRequest = null;
/**
* Response received from the last method call
* @var Zend_XmlRpc_Response
*/
protected $_lastResponse = null;
/**
* Proxy object for more convenient method calls
* @var array of Zend_XmlRpc_Client_ServerProxy
*/
protected $_proxyCache = array();
/**
* Flag for skipping system lookup
* @var bool
*/
protected $_skipSystemLookup = false;
/**
* Create a new XML-RPC client to a remote server
*
* @param string $server Full address of the XML-RPC service
* (e.g. http://time.xmlrpc.com/RPC2)
* @param Zend_Http_Client $httpClient HTTP Client to use for requests
* @return void
*/
public function __construct($server, Zend_Http_Client $httpClient = null)
{
if ($httpClient === null) {
$this->_httpClient = new Zend_Http_Client();
} else {
$this->_httpClient = $httpClient;
}
$this->_introspector = new Zend_XmlRpc_Client_ServerIntrospection($this);
$this->_serverAddress = $server;
}
/**
* Sets the HTTP client object to use for connecting the XML-RPC server.
*
* @param Zend_Http_Client $httpClient
* @return Zend_Http_Client
*/
public function setHttpClient(Zend_Http_Client $httpClient)
{
return $this->_httpClient = $httpClient;
}
/**
* Gets the HTTP client object.
*
* @return Zend_Http_Client
*/
public function getHttpClient()
{
return $this->_httpClient;
}
/**
* Sets the object used to introspect remote servers
*
* @param Zend_XmlRpc_Client_ServerIntrospection
* @return Zend_XmlRpc_Client_ServerIntrospection
*/
public function setIntrospector(Zend_XmlRpc_Client_ServerIntrospection $introspector)
{
return $this->_introspector = $introspector;
}
/**
* Gets the introspection object.
*
* @return Zend_XmlRpc_Client_ServerIntrospection
*/
public function getIntrospector()
{
return $this->_introspector;
}
/**
* The request of the last method call
*
* @return Zend_XmlRpc_Request
*/
public function getLastRequest()
{
return $this->_lastRequest;
}
/**
* The response received from the last method call
*
* @return Zend_XmlRpc_Response
*/
public function getLastResponse()
{
return $this->_lastResponse;
}
/**
* Returns a proxy object for more convenient method calls
*
* @param $namespace Namespace to proxy or empty string for none
* @return Zend_XmlRpc_Client_ServerProxy
*/
public function getProxy($namespace = '')
{
if (empty($this->_proxyCache[$namespace])) {
$proxy = new Zend_XmlRpc_Client_ServerProxy($this, $namespace);
$this->_proxyCache[$namespace] = $proxy;
}
return $this->_proxyCache[$namespace];
}
/**
* Set skip system lookup flag
*
* @param bool $flag
* @return Zend_XmlRpc_Client
*/
public function setSkipSystemLookup($flag = true)
{
$this->_skipSystemLookup = (bool) $flag;
return $this;
}
/**
* Skip system lookup when determining if parameter should be array or struct?
*
* @return bool
*/
public function skipSystemLookup()
{
return $this->_skipSystemLookup;
}
/**
* Perform an XML-RPC request and return a response.
*
* @param Zend_XmlRpc_Request $request
* @param null|Zend_XmlRpc_Response $response
* @return void
* @throws Zend_XmlRpc_Client_HttpException
*/
public function doRequest($request, $response = null)
{
$this->_lastRequest = $request;
iconv_set_encoding('input_encoding', 'UTF-8');
iconv_set_encoding('output_encoding', 'UTF-8');
iconv_set_encoding('internal_encoding', 'UTF-8');
$http = $this->getHttpClient();
if($http->getUri() === null) {
$http->setUri($this->_serverAddress);
}
$http->setHeaders(array(
'Content-Type: text/xml; charset=utf-8',
'Accept: text/xml',
));
if ($http->getHeader('user-agent') === null) {
$http->setHeaders(array('User-Agent: Zend_XmlRpc_Client'));
}
$xml = $this->_lastRequest->__toString();
$http->setRawData($xml);
$httpResponse = $http->request(Zend_Http_Client::POST);
if (! $httpResponse->isSuccessful()) {
/**
* Exception thrown when an HTTP error occurs
* @see Zend_XmlRpc_Client_HttpException
*/
require_once 'Zend/XmlRpc/Client/HttpException.php';
throw new Zend_XmlRpc_Client_HttpException(
$httpResponse->getMessage(),
$httpResponse->getStatus());
}
if ($response === null) {
$response = new Zend_XmlRpc_Response();
}
$this->_lastResponse = $response;
$this->_lastResponse->loadXml($httpResponse->getBody());
}
/**
* Send an XML-RPC request to the service (for a specific method)
*
* @param string $method Name of the method we want to call
* @param array $params Array of parameters for the method
* @return mixed
* @throws Zend_XmlRpc_Client_FaultException
*/
public function call($method, $params=array())
{
if (!$this->skipSystemLookup() && ('system.' != substr($method, 0, 7))) {
// Ensure empty array/struct params are cast correctly
// If system.* methods are not available, bypass. (ZF-2978)
$success = true;
try {
$signatures = $this->getIntrospector()->getMethodSignature($method);
} catch (Zend_XmlRpc_Exception $e) {
$success = false;
}
if ($success) {
$validTypes = array(
Zend_XmlRpc_Value::XMLRPC_TYPE_ARRAY,
Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64,
Zend_XmlRpc_Value::XMLRPC_TYPE_BOOLEAN,
Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME,
Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE,
Zend_XmlRpc_Value::XMLRPC_TYPE_I4,
Zend_XmlRpc_Value::XMLRPC_TYPE_INTEGER,
Zend_XmlRpc_Value::XMLRPC_TYPE_NIL,
Zend_XmlRpc_Value::XMLRPC_TYPE_STRING,
Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT,
);
if (!is_array($params)) {
$params = array($params);
}
foreach ($params as $key => $param) {
if ($param instanceof Zend_XmlRpc_Value) {
continue;
}
$type = Zend_XmlRpc_Value::AUTO_DETECT_TYPE;
foreach ($signatures as $signature) {
if (!is_array($signature)) {
continue;
}
if (isset($signature['parameters'][$key])) {
$type = $signature['parameters'][$key];
$type = in_array($type, $validTypes) ? $type : Zend_XmlRpc_Value::AUTO_DETECT_TYPE;
}
}
$params[$key] = Zend_XmlRpc_Value::getXmlRpcValue($param, $type);
}
}
}
$request = $this->_createRequest($method, $params);
$this->doRequest($request);
if ($this->_lastResponse->isFault()) {
$fault = $this->_lastResponse->getFault();
/**
* Exception thrown when an XML-RPC fault is returned
* @see Zend_XmlRpc_Client_FaultException
*/
require_once 'Zend/XmlRpc/Client/FaultException.php';
throw new Zend_XmlRpc_Client_FaultException($fault->getMessage(),
$fault->getCode());
}
return $this->_lastResponse->getReturnValue();
}
/**
* Create request object
*
* @return Zend_XmlRpc_Request
*/
protected function _createRequest($method, $params)
{
return new Zend_XmlRpc_Request($method, $params);
}
}
|