X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=lib%2FXmlRpcClient.php;h=83a154e933448a37883e4d5e1bf3ffbc32f63823;hp=886198b3662cac58d6a59952acaa686f77432507;hb=ebf03950ffaea849b931adf83b6c20ac9fb7ef33;hpb=bc0d0be07283bb4669e17528e347cd3870656136 diff --git a/lib/XmlRpcClient.php b/lib/XmlRpcClient.php index 886198b..83a154e 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,78 +22,92 @@ * @copyright Michael Wallner, * @license BSD, revised * @package pecl/http - * @version $Revision$ + * @version $Revision$ */ class XmlRpcClient { - /** - * RPC namespace - * - * @var string - */ - public $namespace; - - /** - * HttpRequest instance - * - * @var HttpRequest - */ - protected $request; - - /** - * Constructor - * - * @param string $url RPC endpoint - * @param string $namespace RPC namespace - */ - 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) - { - return $this->request->setOptions($options); - } - - /** - * Get associated HttpRequest instance - * - * @return HttpRequest - */ - public function getRequest() - { - return $this->request; - } - - /** - * RPC method proxy - * - * @param string $method RPC method name - * @param array $params RPC method arguments - * @return mixed decoded RPC response - * @throws Exception - */ - public function __call($method, array $params) - { - if ($this->namespace) { - $method = $this->namespace .'.'. $method; - } - $this->request->setRawPostData(xmlrpc_encode_request($method, $params)); - $response = $this->request->send(); - if ($response->getResponseCode() != 200) { - throw new Exception($response->getBody(), $response->getResponseCode()); - } - return xmlrpc_decode($response->getBody(), 'utf-8'); - } + /** + * RPC namespace + * + * @var string + */ + public $__namespace; + + /** + * HttpRequest instance + * + * @var HttpRequest + */ + 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 = '', array $options = null) + { + $this->__request = new HttpRequest($url, HTTP_METH_POST); + $this->__request->setOptions($options); + $this->__namespace = $namespace; + } + + /** + * RPC method proxy + * + * @param string $method RPC method name + * @param array $params RPC method arguments + * @return mixed decoded RPC response + * @throws Exception + */ + public function __call($method, array $params) + { + if ($this->__namespace) { + $method = $this->__namespace .'.'. $method; + } + $this->__request->setContentType("text/xml; charset=". $this->__encoding); + $request = xmlrpc_encode_request($method, $params, array("encoding" => $this->__encoding)); + $this->__request->setRawPostData($request); + $this->__request->send(); + $response = $this->__request->getResponseMessage(); + if ($response->getResponseCode() != 200) { + 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 $data; + } + + /** + * Returns self, where namespace is set to variable name + * + * @param string $ns + * @return XmlRpcRequest + */ + public function __get($ns) + { + $this->__namespace = $ns; + return $this; + } } ?>