- a service implemented with libxmlrpc-c3 chokes on a request content type
[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 * Constructor
52 *
53 * @param string $url RPC endpoint
54 * @param string $namespace RPC namespace
55 * @param array $options HttpRequest options
56 */
57 public function __construct($url, $namespace = '', array $options = array())
58 {
59 $this->__request = new HttpRequest($url, HttpRequest::METH_POST, $options);
60 $this->__namespace = $namespace;
61 }
62
63 /**
64 * RPC method proxy
65 *
66 * @param string $method RPC method name
67 * @param array $params RPC method arguments
68 * @return mixed decoded RPC response
69 * @throws Exception
70 */
71 public function __call($method, array $params)
72 {
73 if (strlen($this->__namespace)) {
74 $method = $this->__namespace .'.'. $method;
75 }
76 $this->__request->setContentType("text/xml");
77 $this->__request->setRawPostData(
78 xmlrpc_encode_request($method, $params,
79 array("encoding" => $this->__encoding)));
80 $response = $this->__request->send();
81 if ($response->getResponseCode() != 200) {
82 throw new Exception(
83 $response->getResponseStatus(),
84 $response->getResponseCode()
85 );
86 }
87
88 $data = xmlrpc_decode($response->getBody(), $this->__encoding);
89 if (xmlrpc_is_fault($data)) {
90 throw new Exception(
91 (string) $data['faultString'],
92 (int) $data['faultCode']
93 );
94 }
95
96 return $data;
97 }
98
99 /**
100 * Returns self, where namespace is set to variable name
101 *
102 * @param string $ns
103 * @return XmlRpcRequest
104 */
105 public function __get($ns)
106 {
107 $this->__namespace = $ns;
108 return $this;
109 }
110 }
111
112 ?>