X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=lib%2FXmlRpcClient.php;h=488e1cf0c59e91f25f6406acedb56c0b9bc0bc58;hp=f36347d91a1e4cbb5f14f56007f642f61456a251;hb=f39ea2b145caa37d9614a8a437ffd3d8914b52f0;hpb=e78857a010ae86f5dddb593cbe96d67e5bd6f13d diff --git a/lib/XmlRpcClient.php b/lib/XmlRpcClient.php index f36347d..488e1cf 100644 --- a/lib/XmlRpcClient.php +++ b/lib/XmlRpcClient.php @@ -9,9 +9,10 @@ * Usage: * * __request->setOptions(array('compress' => true)); * try { - * print_r($rpc->listdomain(array('domain' => 'example.com')); + * print_r($rpc->vpop->listdomain(array('domain' => 'example.com'))); * } catch (Exception $ex) { * echo $ex; * } @@ -21,58 +22,44 @@ * @copyright Michael Wallner, * @license BSD, revised * @package pecl/http - * @version $Revision$ + * @version $Revision$ */ class XmlRpcClient { - /** - * RPC namespace - * - * @var string - */ - public $namespace; + /** + * RPC namespace + * + * @var string + */ + public $__namespace; /** * HttpRequest instance * * @var HttpRequest */ - protected $request; - + public $__request; + + /** + * Client charset + * + * @var string + */ + public $__encoding = "iso-8859-1"; + /** * Constructor * * @param string $url RPC endpoint * @param string $namespace RPC namespace + * @param array $options HttpRequest options */ - public function __construct($url, $namespace = '') - { - $this->namespace = $namespace; - $this->request = new HttpRequest($url, HTTP_METH_POST); - $this->request->setContentType('text/xml'); - } - - /** - * Proxy to HttpRequest::setOptions() - * - * @param array $options - * @return unknown - */ - public function setOptions(array $options = null) + public function __construct($url, $namespace = '', array $options = array()) { - return $this->request->setOptions($options); + $this->__request = new HttpRequest($url, HttpRequest::METH_POST, $options); + $this->__namespace = $namespace; } - /** - * Get associated HttpRequest instance - * - * @return HttpRequest - */ - public function getRequest() - { - return $this->request; - } - /** * RPC method proxy * @@ -83,15 +70,42 @@ class XmlRpcClient */ public function __call($method, array $params) { - if ($this->namespace) { - $method = $this->namespace .'.'. $method; + if (strlen($this->__namespace)) { + $method = $this->__namespace .'.'. $method; } - $this->request->setRawPostData(xmlrpc_encode_request($method, $params)); - $response = $this->request->send(); + $this->__request->setContentType("text/xml"); + $this->__request->setRawPostData( + xmlrpc_encode_request($method, $params, + array("encoding" => $this->__encoding))); + $response = $this->__request->send(); if ($response->getResponseCode() != 200) { - throw new Exception($response->getBody(), $response->getResponseCode()); + throw new Exception( + $response->getResponseStatus(), + $response->getResponseCode() + ); + } + + $data = xmlrpc_decode($response->getBody(), $this->__encoding); + if (xmlrpc_is_fault($data)) { + throw new Exception( + (string) $data['faultString'], + (int) $data['faultCode'] + ); } - return xmlrpc_decode($response->getBody(), 'utf-8'); + + return $data; + } + + /** + * Returns self, where namespace is set to variable name + * + * @param string $ns + * @return XmlRpcRequest + */ + public function __get($ns) + { + $this->__namespace = $ns; + return $this; } }