f36347d91a1e4cbb5f14f56007f642f61456a251
[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 * try {
14 * print_r($rpc->listdomain(array('domain' => 'example.com'));
15 * } catch (Exception $ex) {
16 * echo $ex;
17 * }
18 * ?>
19 * </code>
20 *
21 * @copyright Michael Wallner, <mike@iworks.at>
22 * @license BSD, revised
23 * @package pecl/http
24 * @version $Revision$
25 */
26 class XmlRpcClient
27 {
28 /**
29 * RPC namespace
30 *
31 * @var string
32 */
33 public $namespace;
34
35 /**
36 * HttpRequest instance
37 *
38 * @var HttpRequest
39 */
40 protected $request;
41
42 /**
43 * Constructor
44 *
45 * @param string $url RPC endpoint
46 * @param string $namespace RPC namespace
47 */
48 public function __construct($url, $namespace = '')
49 {
50 $this->namespace = $namespace;
51 $this->request = new HttpRequest($url, HTTP_METH_POST);
52 $this->request->setContentType('text/xml');
53 }
54
55 /**
56 * Proxy to HttpRequest::setOptions()
57 *
58 * @param array $options
59 * @return unknown
60 */
61 public function setOptions(array $options = null)
62 {
63 return $this->request->setOptions($options);
64 }
65
66 /**
67 * Get associated HttpRequest instance
68 *
69 * @return HttpRequest
70 */
71 public function getRequest()
72 {
73 return $this->request;
74 }
75
76 /**
77 * RPC method proxy
78 *
79 * @param string $method RPC method name
80 * @param array $params RPC method arguments
81 * @return mixed decoded RPC response
82 * @throws Exception
83 */
84 public function __call($method, array $params)
85 {
86 if ($this->namespace) {
87 $method = $this->namespace .'.'. $method;
88 }
89 $this->request->setRawPostData(xmlrpc_encode_request($method, $params));
90 $response = $this->request->send();
91 if ($response->getResponseCode() != 200) {
92 throw new Exception($response->getBody(), $response->getResponseCode());
93 }
94 return xmlrpc_decode($response->getBody(), 'utf-8');
95 }
96 }
97
98 ?>