release 1.7.0b1
[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');
13 * $rpc->__request->setOptions(array('compress' => true));
14 * try {
15 * print_r($rpc->vpop->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 * Client charset
45 *
46 * @var string
47 */
48 public $__encoding = "iso-8859-1";
49
50 /**
51 * RPC options
52 *
53 * @var array
54 */
55 public $__options;
56
57 /**
58 * Constructor
59 *
60 * @param string $url RPC endpoint
61 * @param string $namespace RPC namespace
62 * @param array $options HttpRequest options
63 */
64 public function __construct($url, $namespace = '', array $options = null)
65 {
66 $this->__request = new HttpRequest($url, HttpRequest::METH_POST, (array) $options);
67 $this->__namespace = $namespace;
68 }
69
70 /**
71 * RPC method proxy
72 *
73 * @param string $method RPC method name
74 * @param array $params RPC method arguments
75 * @return mixed decoded RPC response
76 * @throws Exception
77 */
78 public function __call($method, array $params)
79 {
80 if (strlen($this->__namespace)) {
81 $method = $this->__namespace .'.'. $method;
82 }
83 $this->__request->setContentType("text/xml");
84 $this->__request->setRawPostData(
85 xmlrpc_encode_request($method, $params,
86 array("encoding" => $this->__encoding) + (array) $this->__options));
87 $response = $this->__request->send();
88 if ($response->getResponseCode() != 200) {
89 throw new Exception(
90 $response->getResponseStatus(),
91 $response->getResponseCode()
92 );
93 }
94
95 $data = xmlrpc_decode($response->getBody(), $this->__encoding);
96 if (xmlrpc_is_fault($data)) {
97 throw new Exception(
98 (string) $data['faultString'],
99 (int) $data['faultCode']
100 );
101 }
102
103 return $data;
104 }
105
106 /**
107 * Returns self, where namespace is set to variable name
108 *
109 * @param string $ns
110 * @return XmlRpcRequest
111 */
112 public function __get($ns)
113 {
114 $this->__namespace = $ns;
115 return $this;
116 }
117 }
118
119 ?>