| 1 |
lars |
1 |
#LyX 1.3 created this file. For more info see http://www.lyx.org/
|
|
|
2 |
\lyxformat 221
|
|
|
3 |
\textclass docbook-section
|
|
|
4 |
\language english
|
|
|
5 |
\inputencoding auto
|
|
|
6 |
\fontscheme default
|
|
|
7 |
\graphics default
|
|
|
8 |
\paperfontsize default
|
|
|
9 |
\spacing single
|
|
|
10 |
\papersize Default
|
|
|
11 |
\paperpackage a4
|
|
|
12 |
\use_geometry 0
|
|
|
13 |
\use_amsmath 0
|
|
|
14 |
\use_natbib 0
|
|
|
15 |
\use_numerical_citations 0
|
|
|
16 |
\paperorientation portrait
|
|
|
17 |
\secnumdepth 3
|
|
|
18 |
\tocdepth 3
|
|
|
19 |
\paragraph_separation indent
|
|
|
20 |
\defskip medskip
|
|
|
21 |
\quotes_language english
|
|
|
22 |
\quotes_times 2
|
|
|
23 |
\papercolumns 1
|
|
|
24 |
\papersides 1
|
|
|
25 |
\paperpagestyle default
|
|
|
26 |
|
|
|
27 |
\layout Title
|
|
|
28 |
\added_space_top vfill \added_space_bottom vfill
|
|
|
29 |
XML_RPC2 Tutorial
|
|
|
30 |
\layout Abstract
|
|
|
31 |
|
|
|
32 |
This tutorial introduces basic usage of XML_RPC2 as a client/server library
|
|
|
33 |
in XML_RPC operations.
|
|
|
34 |
XML_RPC2 is a pear package providing XML_RPC client and server services.
|
|
|
35 |
XML-RPC is a simple remote procedure call protocol built using HTTP as
|
|
|
36 |
transport and XML as encoding.
|
|
|
37 |
\layout Abstract
|
|
|
38 |
|
|
|
39 |
As a client library, XML_RPC2 is capable of creating a proxy class which
|
|
|
40 |
exposes the methods exported by the server.
|
|
|
41 |
As a server library, XML_RPC2 is capable of exposing methods from a class
|
|
|
42 |
or object instance, seamlessly exporting local methods as remotely callable
|
|
|
43 |
procedures.
|
|
|
44 |
|
|
|
45 |
\layout Subsection
|
|
|
46 |
|
|
|
47 |
Client usage
|
|
|
48 |
\layout Subsubsection
|
|
|
49 |
|
|
|
50 |
Basic Usage
|
|
|
51 |
\layout Standard
|
|
|
52 |
|
|
|
53 |
The most simple way to use the XML_RPC client is by letting XML_RPC2 select
|
|
|
54 |
the backend for you, and just give the client factory method the data referring
|
|
|
55 |
to the server:
|
|
|
56 |
\layout Itemize
|
|
|
57 |
|
|
|
58 |
The server URI.
|
|
|
59 |
\layout Itemize
|
|
|
60 |
|
|
|
61 |
The HTTP proxy URI (null if no proxy).
|
|
|
62 |
\layout Itemize
|
|
|
63 |
|
|
|
64 |
The method prefix
|
|
|
65 |
\layout Code
|
|
|
66 |
|
|
|
67 |
require_once('XML/RPC2/Client.php');
|
|
|
68 |
\layout Code
|
|
|
69 |
|
|
|
70 |
$client = XML_RPC2_Client::create('http://rpc.example.com:80/', null, '');
|
|
|
71 |
\layout Standard
|
|
|
72 |
|
|
|
73 |
The factory will produce a client proxy.
|
|
|
74 |
This class exports whichever methods the server exports.
|
|
|
75 |
These methods are called just like regular local methods:
|
|
|
76 |
\layout Code
|
|
|
77 |
|
|
|
78 |
print($client->hello('World'));
|
|
|
79 |
\layout Standard
|
|
|
80 |
|
|
|
81 |
for a server that exports the method hello.
|
|
|
82 |
If the server has methods prefixed by a classname (example.hello), there
|
|
|
83 |
are two solutions.
|
|
|
84 |
Either call the method using brackets enclosing the otherwise php-invalid
|
|
|
85 |
method name:
|
|
|
86 |
\layout Code
|
|
|
87 |
|
|
|
88 |
print($client->{example.hello}('World'));
|
|
|
89 |
\layout Standard
|
|
|
90 |
|
|
|
91 |
Or specify a method prefix when creating the client instance:
|
|
|
92 |
\layout Code
|
|
|
93 |
|
|
|
94 |
$client = XML_RPC2_Client::create('http://rpc.example.com:80/', null, 'example.');
|
|
|
95 |
\layout Code
|
|
|
96 |
|
|
|
97 |
print($client->hello('World'));
|
|
|
98 |
\layout Subsubsection
|
|
|
99 |
|
|
|
100 |
Error handling
|
|
|
101 |
\layout Standard
|
|
|
102 |
|
|
|
103 |
XML_RPC2 uses exceptions to signal errors.
|
|
|
104 |
The phpdoc reference contains a class hierarchy useful to get a grasp of
|
|
|
105 |
possible errors.
|
|
|
106 |
The most important characteristics of the XML_RPC2 exception tree are:
|
|
|
107 |
\layout Itemize
|
|
|
108 |
|
|
|
109 |
All XML_RPC2 exceptions are children of XML_RPC2_Exception.
|
|
|
110 |
If you want to filter out exceptions from this package, catch XML_RPC2_Exceptio
|
|
|
111 |
n
|
|
|
112 |
\layout Itemize
|
|
|
113 |
|
|
|
114 |
Network failure is signaled by an XML_RPC2_TransportException
|
|
|
115 |
\layout Itemize
|
|
|
116 |
|
|
|
117 |
Regular XML-RPC fault responses are signaled by an XML_RPC2_FaultException
|
|
|
118 |
\layout Itemize
|
|
|
119 |
|
|
|
120 |
All other types of XML_RPC2_Exception signal package misuse or bug-induced
|
|
|
121 |
misbehaviour
|
|
|
122 |
\layout Standard
|
|
|
123 |
|
|
|
124 |
Standard usage:
|
|
|
125 |
\layout Code
|
|
|
126 |
|
|
|
127 |
require_once('XMLrequire_once('XML/RPC2/Client.php');
|
|
|
128 |
\layout Code
|
|
|
129 |
|
|
|
130 |
try {
|
|
|
131 |
\layout Code
|
|
|
132 |
|
|
|
133 |
$client = XML_RPC2_Client::create('http://rpc.example.com:80/', null,
|
|
|
134 |
'');
|
|
|
135 |
\layout Code
|
|
|
136 |
|
|
|
137 |
print($client->hello('World'));
|
|
|
138 |
\layout Code
|
|
|
139 |
|
|
|
140 |
} catch (XML_RPC2_TransportException transportException) {
|
|
|
141 |
\layout Code
|
|
|
142 |
|
|
|
143 |
// Handle network-induced exception
|
|
|
144 |
\layout Code
|
|
|
145 |
|
|
|
146 |
} catch (XML_RPC2_FaultException fault) {
|
|
|
147 |
\layout Code
|
|
|
148 |
|
|
|
149 |
// Handle fault returned by remote server
|
|
|
150 |
\layout Code
|
|
|
151 |
|
|
|
152 |
} catch (XML_RPC2_Exception xmlRpcException) {
|
|
|
153 |
\layout Code
|
|
|
154 |
|
|
|
155 |
// Handle abnormal XML_RPC2 package exception
|
|
|
156 |
\layout Code
|
|
|
157 |
|
|
|
158 |
} catch (Exception e) {
|
|
|
159 |
\layout Code
|
|
|
160 |
|
|
|
161 |
// Handle someone else's fault exception
|
|
|
162 |
\layout Code
|
|
|
163 |
|
|
|
164 |
}
|
|
|
165 |
\layout Standard
|
|
|
166 |
|
|
|
167 |
It is good practice to at least expect XML_RPC2_TransportException as network
|
|
|
168 |
failure can't ever be ruled out.
|
|
|
169 |
|
|
|
170 |
\layout Subsection
|
|
|
171 |
|
|
|
172 |
Server usage
|
|
|
173 |
\layout Subsubsection
|
|
|
174 |
|
|
|
175 |
Basic Usage
|
|
|
176 |
\layout Standard
|
|
|
177 |
|
|
|
178 |
To export an XML-RPC server using XML_RPC2, the first step is writing the
|
|
|
179 |
methods to export.
|
|
|
180 |
XML_RPC2 can export class methods (static methods) for a class, or all
|
|
|
181 |
methods for an object instance.
|
|
|
182 |
For this example, we'll export a class' static methods:
|
|
|
183 |
\layout Code
|
|
|
184 |
|
|
|
185 |
class EchoServer {
|
|
|
186 |
\layout Code
|
|
|
187 |
|
|
|
188 |
/**
|
|
|
189 |
\layout Code
|
|
|
190 |
|
|
|
191 |
* echoecho echoes the message received
|
|
|
192 |
\layout Code
|
|
|
193 |
|
|
|
194 |
*
|
|
|
195 |
\layout Code
|
|
|
196 |
|
|
|
197 |
* @param string Message
|
|
|
198 |
\layout Code
|
|
|
199 |
|
|
|
200 |
* @return string The echo
|
|
|
201 |
\layout Code
|
|
|
202 |
|
|
|
203 |
*/
|
|
|
204 |
\layout Code
|
|
|
205 |
|
|
|
206 |
\layout Code
|
|
|
207 |
|
|
|
208 |
public static function echoecho($string)
|
|
|
209 |
\layout Code
|
|
|
210 |
|
|
|
211 |
{
|
|
|
212 |
\layout Code
|
|
|
213 |
|
|
|
214 |
return $string;
|
|
|
215 |
\layout Code
|
|
|
216 |
|
|
|
217 |
}
|
|
|
218 |
\layout Code
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
\layout Code
|
|
|
222 |
|
|
|
223 |
/**
|
|
|
224 |
\layout Code
|
|
|
225 |
|
|
|
226 |
* Dummy method which won't be exported
|
|
|
227 |
\layout Code
|
|
|
228 |
|
|
|
229 |
*
|
|
|
230 |
\layout Code
|
|
|
231 |
|
|
|
232 |
* @xmlrpc.hidden
|
|
|
233 |
\layout Code
|
|
|
234 |
|
|
|
235 |
*/
|
|
|
236 |
\layout Code
|
|
|
237 |
|
|
|
238 |
public static function dummy()
|
|
|
239 |
\layout Code
|
|
|
240 |
|
|
|
241 |
{
|
|
|
242 |
\layout Code
|
|
|
243 |
|
|
|
244 |
return false;
|
|
|
245 |
\layout Code
|
|
|
246 |
|
|
|
247 |
}
|
|
|
248 |
\layout Code
|
|
|
249 |
|
|
|
250 |
\layout Code
|
|
|
251 |
|
|
|
252 |
/**
|
|
|
253 |
\layout Code
|
|
|
254 |
|
|
|
255 |
* hello says hello
|
|
|
256 |
\layout Code
|
|
|
257 |
|
|
|
258 |
*
|
|
|
259 |
\layout Code
|
|
|
260 |
|
|
|
261 |
* @param string Name
|
|
|
262 |
\layout Code
|
|
|
263 |
|
|
|
264 |
* @return string Hello 'name'
|
|
|
265 |
\layout Code
|
|
|
266 |
|
|
|
267 |
*/
|
|
|
268 |
\layout Code
|
|
|
269 |
|
|
|
270 |
public function hello($name)
|
|
|
271 |
\layout Code
|
|
|
272 |
|
|
|
273 |
{
|
|
|
274 |
\layout Code
|
|
|
275 |
|
|
|
276 |
return "Hello $name";
|
|
|
277 |
\layout Code
|
|
|
278 |
|
|
|
279 |
}
|
|
|
280 |
\layout Code
|
|
|
281 |
|
|
|
282 |
\layout Code
|
|
|
283 |
|
|
|
284 |
}
|
|
|
285 |
\layout Standard
|
|
|
286 |
|
|
|
287 |
Note that the method is documented using phpDoc docblocks.
|
|
|
288 |
The docblock is used to deduce the signature and method documentation,
|
|
|
289 |
required by the XML-RPC spec.
|
|
|
290 |
Non-documented methods are not exported.
|
|
|
291 |
Methods tagged with the tag @xmlrpc.hidden are not exported either (the
|
|
|
292 |
dummy method above won't be exported).
|
|
|
293 |
\layout Standard
|
|
|
294 |
|
|
|
295 |
After creating the class, we need to get an XML_RPC2 server to export its
|
|
|
296 |
methods remotely:
|
|
|
297 |
\layout Code
|
|
|
298 |
|
|
|
299 |
require_once 'XML/RPC2/Server.php';
|
|
|
300 |
\layout Code
|
|
|
301 |
|
|
|
302 |
$server = XML_RPC2_Server::create('EchoServer');
|
|
|
303 |
\layout Code
|
|
|
304 |
|
|
|
305 |
$server->handleCall();
|
|
|
306 |
\layout Standard
|
|
|
307 |
|
|
|
308 |
The XML_RPC2_Server automatically exports all of the EchoServer class public
|
|
|
309 |
static methods (echoecho in this case).
|
|
|
310 |
You may also export all of an instance's public methods (static or otherwise):
|
|
|
311 |
\layout Code
|
|
|
312 |
|
|
|
313 |
require_once 'XML/RPC2/Server.php';
|
|
|
314 |
\layout Code
|
|
|
315 |
|
|
|
316 |
$server = XML_RPC2_Server::create(new EchoServer());
|
|
|
317 |
\layout Code
|
|
|
318 |
|
|
|
319 |
$server->handleCall();
|
|
|
320 |
\the_end
|