- cleanup
[m6w6/ext-http] / lib / XmlRpcClient.php
1 <?php
2
3 /**
4 * XMLRPC Client, very KISS
5 * $Id$
6 *
7 * NOTE: requires ext/xmlrpc
8 *
9 * Usage:
10 * <code>
11 * <?php
12 * $rpc = new XmlRpcClient('http://mike:secret@example.com/cgi-bin/vpop-xmlrpc', 'vpop');
13 * $rpc->options = array(array('compress' => true));
14 * try {
15 * print_r($rpc->listdomain(array('domain' => 'example.com')));
16 * } catch (Exception $ex) {
17 * echo $ex;
18 * }
19 * ?>
20 * </code>
21 *
22 * @copyright Michael Wallner, <mike@iworks.at>
23 * @license BSD, revised
24 * @package pecl/http
25 * @version $Revision$
26 */
27 class XmlRpcClient
28 {
29 /**
30 * RPC namespace
31 *
32 * @var string
33 */
34 public $namespace;
35
36 /**
37 * HttpRequest instance
38 *
39 * @var HttpRequest
40 */
41 public $request;
42
43 /**
44 * Constructor
45 *
46 * @param string $url RPC endpoint
47 * @param string $namespace RPC namespace
48 */
49 public function __construct($url, $namespace = '')
50 {
51 $this->namespace = $namespace;
52 $this->request = new HttpRequest($url, HTTP_METH_POST);
53 $this->request->setContentType('text/xml');
54 }
55
56 /**
57 * RPC method proxy
58 *
59 * @param string $method RPC method name
60 * @param array $params RPC method arguments
61 * @return mixed decoded RPC response
62 * @throws Exception
63 */
64 public function __call($method, array $params)
65 {
66 if ($this->namespace) {
67 $method = $this->namespace .'.'. $method;
68 }
69
70 $data = xmlrpc_encode_request($method, $params);
71 $this->request->setRawPostData($data);
72
73 $response = $this->request->send();
74 if ($response->getResponseCode() != 200) {
75 throw new Exception(
76 $response->getResponseStatus(),
77 $response->getResponseCode()
78 );
79 }
80 $data = xmlrpc_decode($response->getBody(), 'utf-8');
81
82 if (isset($data['faultCode'], $data['faultString'])) {
83 throw new Exception(
84 $data['faultString'],
85 $data['faultCode']
86 );
87 }
88
89 return $data;
90 }
91
92 public function __set($what, $params)
93 {
94 return call_user_func_array(
95 array($this->request, "set$what"),
96 $params
97 );
98 }
99
100 public function __get($what)
101 {
102 return call_user_func(
103 array($this->request, "get$what")
104 );
105 }
106 }
107
108 ?>