- cleanup
[m6w6/ext-http] / lib / XmlRpcClient.php
index f36347d91a1e4cbb5f14f56007f642f61456a251..9cdc7bbd54151115576e730556f6be20a6fb6496 100644 (file)
@@ -10,8 +10,9 @@
  * <code>
  * <?php
  * $rpc = new XmlRpcClient('http://mike:secret@example.com/cgi-bin/vpop-xmlrpc', 'vpop');
+ * $rpc->options = array(array('compress' => true));
  * try {
- *     print_r($rpc->listdomain(array('domain' => 'example.com'));
+ *     print_r($rpc->listdomain(array('domain' => 'example.com')));
  * } catch (Exception $ex) {
  *     echo $ex;
  * }
@@ -30,69 +31,78 @@ class XmlRpcClient
      *
      * @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');
-       }
+    public $namespace;
+    
+    /**
+     * HttpRequest instance
+     *
+     * @var HttpRequest
+     */
+    public $request;
 
-       /**
-        * 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;
-       }
+    /**
+     * 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;
-               }
-               $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 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")
+        );
+    }
 }
 
 ?>