- improve handling of xmlrpc request/response options
[m6w6/ext-http] / lib / XmlRpcClient.php
index f36347d91a1e4cbb5f14f56007f642f61456a251..8668291bd0a31cb7566f5dad25bd2c026b4ff6c3 100644 (file)
@@ -9,9 +9,10 @@
  * Usage:
  * <code>
  * <?php
- * $rpc = new XmlRpcClient('http://mike:secret@example.com/cgi-bin/vpop-xmlrpc', 'vpop');
+ * $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;
  * }
  * @copyright   Michael Wallner, <mike@iworks.at>
  * @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;
+       
        /**
-        * Constructor
-        *
-        * @param string $url RPC endpoint
-        * @param string $namespace RPC namespace
+        * Client charset
+        * 
+        * @var string
         */
-       public function __construct($url, $namespace = '')
-       {
-               $this->namespace = $namespace;
-               $this->request = new HttpRequest($url, HTTP_METH_POST);
-               $this->request->setContentType('text/xml');
-       }
-
+       public $__encoding = "iso-8859-1";
+       
        /**
-        * Proxy to HttpRequest::setOptions()
-        *
-        * @param array $options
-        * @return unknown
+        * RPC options
+        * 
+        * @var array
         */
-       public function setOptions(array $options = null)
-       {
-               return $this->request->setOptions($options);
-       }
+       public $__options;
        
        /**
-        * Get associated HttpRequest instance
+        * Constructor
         *
-        * @return HttpRequest
+        * @param string $url RPC endpoint
+        * @param string $namespace RPC namespace
+        * @param array  $options HttpRequest options
         */
-       public function getRequest()
+       public function __construct($url, $namespace = '', array $options = null)
        {
-           return $this->request;
+               $this->__request = new HttpRequest($url, HttpRequest::METH_POST, (array) $options);
+               $this->__namespace = $namespace;
        }
-
+       
        /**
         * RPC method proxy
         *
@@ -83,15 +77,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) + (array) $this->__options));
+               $response = $this->__request->send();
                if ($response->getResponseCode() != 200) {
-                       throw new Exception($response->getBody(), $response->getResponseCode());
+                       throw new Exception(
+                               $response->getResponseStatus(), 
+                               $response->getResponseCode()
+                       );
                }
-               return xmlrpc_decode($response->getBody(), 'utf-8');
+               
+               $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;
        }
 }