X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=lib%2FXmlRpcClient.php;h=8668291bd0a31cb7566f5dad25bd2c026b4ff6c3;hp=9cdc7bbd54151115576e730556f6be20a6fb6496;hb=246e63053d5a04be443015d71a52181da80054ac;hpb=e2d31d8ae762ed3acb0ef5ec21edfcc94293dfaf diff --git a/lib/XmlRpcClient.php b/lib/XmlRpcClient.php index 9cdc7bb..8668291 100644 --- a/lib/XmlRpcClient.php +++ b/lib/XmlRpcClient.php @@ -9,10 +9,10 @@ * Usage: * * options = array(array('compress' => true)); + * $rpc = new XmlRpcClient('http://mike:secret@example.com/cgi-bin/vpop-xmlrpc'); + * $rpc->__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; * } @@ -22,87 +22,98 @@ * @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 - */ - public $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'); - } - - /** - * 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; - } - - $data = xmlrpc_encode_request($method, $params); - $this->request->setRawPostData($data); - - $response = $this->request->send(); - if ($response->getResponseCode() != 200) { - throw new Exception( - $response->getResponseStatus(), - $response->getResponseCode() - ); - } - $data = xmlrpc_decode($response->getBody(), 'utf-8'); - - if (isset($data['faultCode'], $data['faultString'])) { - throw new Exception( - $data['faultString'], - $data['faultCode'] - ); - } - - return $data; - } - - public function __set($what, $params) - { - return call_user_func_array( - array($this->request, "set$what"), - $params - ); - } - - public function __get($what) - { - return call_user_func( - array($this->request, "get$what") - ); - } + /** + * RPC namespace + * + * @var string + */ + public $__namespace; + + /** + * HttpRequest instance + * + * @var HttpRequest + */ + public $__request; + + /** + * Client charset + * + * @var string + */ + public $__encoding = "iso-8859-1"; + + /** + * RPC options + * + * @var array + */ + public $__options; + + /** + * 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, HttpRequest::METH_POST, (array) $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 (strlen($this->__namespace)) { + $method = $this->__namespace .'.'. $method; + } + $this->__request->setContentType("text/xml"); + $this->__request->setRawPostData( + xmlrpc_encode_request($method, $params, + array("encoding" => $this->__encoding) + (array) $this->__options)); + $response = $this->__request->send(); + 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; + } } ?>