2
0

Zend_XmlRpc_Server.xml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.xmlrpc.server">
  4. <title>Zend_XmlRpc_Server</title>
  5. <sect2 id="zend.xmlrpc.server.introduction">
  6. <title>Introduction</title>
  7. <para><classname>Zend_XmlRpc_Server</classname> is intended as a fully-featured XML-RPC
  8. server,
  9. following <ulink url="http://www.xmlrpc.com/spec">the specifications
  10. outlined at www.xmlrpc.com</ulink>. Additionally, it implements the
  11. system.multicall() method, allowing boxcarring of requests.
  12. </para>
  13. </sect2>
  14. <sect2 id="zend.xmlrpc.server.usage">
  15. <title>Basic Usage</title>
  16. <para>
  17. An example of the most basic use case:
  18. </para>
  19. <programlisting language="php"><![CDATA[
  20. $server = new Zend_XmlRpc_Server();
  21. $server->setClass('My_Service_Class');
  22. echo $server->handle();
  23. ]]></programlisting>
  24. </sect2>
  25. <sect2 id="zend.xmlrpc.server.structure">
  26. <title>Server Structure</title>
  27. <para>
  28. <classname>Zend_XmlRpc_Server</classname> is composed of a variety of components,
  29. ranging from the server itself to request, response, and fault objects.
  30. </para>
  31. <para>
  32. To bootstrap <classname>Zend_XmlRpc_Server</classname>, the developer must attach one or
  33. more classes or functions to the server, via the
  34. <code>setClass()</code> and <code>addFunction()</code> methods.
  35. </para>
  36. <para>
  37. Once done, you may either pass a <classname>Zend_XmlRpc_Request</classname>
  38. object to <classname>Zend_XmlRpc_Server::handle()</classname>, or it will
  39. instantiate a <classname>Zend_XmlRpc_Request_Http</classname> object if none
  40. is provided -- thus grabbing the request from
  41. <code>php://input</code>.
  42. </para>
  43. <para>
  44. <classname>Zend_XmlRpc_Server::handle()</classname> then attempts to
  45. dispatch to the appropriate handler based on the method
  46. requested. It then returns either a
  47. <classname>Zend_XmlRpc_Response</classname>-based object or a
  48. <classname>Zend_XmlRpc_Server_Fault</classname>object. These objects both have
  49. <code>__toString()</code> methods that create valid XML-RPC XML
  50. responses, allowing them to be directly echoed.
  51. </para>
  52. </sect2>
  53. <sect2 id="zend.xmlrpc.server.conventions">
  54. <title>Conventions</title>
  55. <para>
  56. <classname>Zend_XmlRpc_Server</classname> allows the developer to attach functions and
  57. class method calls as dispatchable XML-RPC methods. Via
  58. <classname>Zend_Server_Reflection</classname>, it does introspection on all attached
  59. methods, using the function and method docblocks to determine the
  60. method help text and method signatures.
  61. </para>
  62. <para>
  63. XML-RPC types do not necessarily map one-to-one to PHP types.
  64. However, the code will do its best to guess the appropriate type
  65. based on the values listed in @param and @return lines. Some XML-RPC
  66. types have no immediate PHP equivalent, however, and should be
  67. hinted using the XML-RPC type in the PHPDoc. These include:
  68. </para>
  69. <itemizedlist>
  70. <listitem><para>dateTime.iso8601, a string formatted as
  71. YYYYMMDDTHH:mm:ss</para></listitem>
  72. <listitem><para>base64, base64 encoded data</para></listitem>
  73. <listitem><para>struct, any associative array</para></listitem>
  74. </itemizedlist>
  75. <para>
  76. An example of how to hint follows:
  77. </para>
  78. <programlisting language="php"><![CDATA[
  79. /**
  80. * This is a sample function
  81. *
  82. * @param base64 $val1 Base64-encoded data
  83. * @param dateTime.iso8601 $val2 An ISO date
  84. * @param struct $val3 An associative array
  85. * @return struct
  86. */
  87. function myFunc($val1, $val2, $val3)
  88. {
  89. }
  90. ]]></programlisting>
  91. <para>
  92. PhpDocumentor does no validation of the types specified for params
  93. or return values, so this will have no impact on your API
  94. documentation. Providing the hinting is necessary, however, when the
  95. server is validating the parameters provided to the method call.
  96. </para>
  97. <para>
  98. It is perfectly valid to specify multiple types for both params and
  99. return values; the XML-RPC specification even suggests that
  100. system.methodSignature should return an array of all possible method
  101. signatures (i.e., all possible combinations of param and return
  102. values). You may do so just as you normally would with
  103. PhpDocumentor, using the '|' operator:
  104. </para>
  105. <programlisting language="php"><![CDATA[
  106. /**
  107. * This is a sample function
  108. *
  109. * @param string|base64 $val1 String or base64-encoded data
  110. * @param string|dateTime.iso8601 $val2 String or an ISO date
  111. * @param array|struct $val3 Normal indexed array or an associative array
  112. * @return boolean|struct
  113. */
  114. function myFunc($val1, $val2, $val3)
  115. {
  116. }
  117. ]]></programlisting>
  118. <para>
  119. One note, however: allowing multiple signatures can lead to
  120. confusion for developers using the services; generally speaking, an
  121. XML-RPC method should only have a single signature.
  122. </para>
  123. </sect2>
  124. <sect2 id="zend.xmlrpc.server.namespaces">
  125. <title>Utilizing Namespaces</title>
  126. <para>
  127. XML-RPC has a concept of namespacing; basically, it allows grouping
  128. XML-RPC methods by dot-delimited namespaces. This helps prevent
  129. naming collisions between methods served by different classes. As an
  130. example, the XML-RPC server is expected to server several methods in
  131. the 'system' namespace:
  132. </para>
  133. <itemizedlist>
  134. <listitem><para>system.listMethods</para></listitem>
  135. <listitem><para>system.methodHelp</para></listitem>
  136. <listitem><para>system.methodSignature</para></listitem>
  137. </itemizedlist>
  138. <para>
  139. Internally, these map to the methods of the same name in
  140. <classname>Zend_XmlRpc_Server</classname>.
  141. </para>
  142. <para>
  143. If you want to add namespaces to the methods you serve, simply
  144. provide a namespace to the appropriate method when attaching a
  145. function or class:
  146. </para>
  147. <programlisting language="php"><![CDATA[
  148. // All public methods in My_Service_Class will be accessible as
  149. // myservice.METHODNAME
  150. $server->setClass('My_Service_Class', 'myservice');
  151. // Function 'somefunc' will be accessible as funcs.somefunc
  152. $server->addFunction('somefunc', 'funcs');
  153. ]]></programlisting>
  154. </sect2>
  155. <sect2 id="zend.xmlrpc.server.request">
  156. <title>Custom Request Objects</title>
  157. <para>
  158. Most of the time, you'll simply use the default request type included with
  159. <classname>Zend_XmlRpc_Server</classname>,
  160. <classname>Zend_XmlRpc_Request_Http</classname>. However, there may be times when you
  161. need XML-RPC to be available via the CLI, a GUI, or other environment, or want to log
  162. incoming requests. To do so, you may create a custom request object that extends
  163. <classname>Zend_XmlRpc_Request</classname>. The most important thing to remember is to
  164. ensure that the getMethod() and getParams() methods are implemented
  165. so that the XML-RPC server can retrieve that information in order to
  166. dispatch the request.
  167. </para>
  168. </sect2>
  169. <sect2 id="zend.xmlrpc.server.response">
  170. <title>Custom Responses</title>
  171. <para>
  172. Similar to request objects, <classname>Zend_XmlRpc_Server</classname> can return custom
  173. response objects; by default, a <classname>Zend_XmlRpc_Response_Http</classname> object
  174. is returned, which sends an appropriate Content-Type HTTP header for
  175. use with XML-RPC. Possible uses of a custom object would be to log
  176. responses, or to send responses back to STDOUT.
  177. </para>
  178. <para>
  179. To use a custom response class, use
  180. <classname>Zend_XmlRpc_Server::setResponseClass()</classname> prior to calling
  181. <code>handle()</code>.
  182. </para>
  183. </sect2>
  184. <sect2 id="zend.xmlrpc.server.fault">
  185. <title>Handling Exceptions via Faults</title>
  186. <para>
  187. <classname>Zend_XmlRpc_Server</classname> catches Exceptions generated by a dispatched
  188. method, and generates an XML-RPC fault response when such an
  189. exception is caught. By default, however, the exception messages and
  190. codes are not used in a fault response. This is an intentional
  191. decision to protect your code; many exceptions expose more
  192. information about the code or environment than a developer would
  193. necessarily intend (a prime example includes database abstraction or
  194. access layer exceptions).
  195. </para>
  196. <para>
  197. Exception classes can be whitelisted to be used as fault responses,
  198. however. To do so, simply utilize
  199. <classname>Zend_XmlRpc_Server_Fault::attachFaultException()</classname> to pass an
  200. exception class to whitelist:
  201. </para>
  202. <programlisting language="php"><![CDATA[
  203. Zend_XmlRpc_Server_Fault::attachFaultException('My_Project_Exception');
  204. ]]></programlisting>
  205. <para>
  206. If you utilize an exception class that your other project exceptions
  207. inherit, you can then whitelist a whole family of exceptions at a
  208. time. <classname>Zend_XmlRpc_Server_Exception</classname>s are always whitelisted, to
  209. allow reporting specific internal errors (undefined methods, etc.).
  210. </para>
  211. <para>
  212. Any exception not specifically whitelisted will generate a fault
  213. response with a code of '404' and a message of 'Unknown error'.
  214. </para>
  215. </sect2>
  216. <sect2 id="zend.xmlrpc.server.caching">
  217. <title>Caching Server Definitions Between Requests</title>
  218. <para>
  219. Attaching many classes to an XML-RPC server instance can utilize a
  220. lot of resources; each class must introspect using the Reflection
  221. API (via <classname>Zend_Server_Reflection</classname>), which in turn generates a list
  222. of all possible method signatures to provide to the server class.
  223. </para>
  224. <para>
  225. To reduce this performance hit somewhat, <classname>Zend_XmlRpc_Server_Cache</classname>
  226. can be used to cache the server definition between requests. When
  227. combined with __autoload(), this can greatly increase performance.
  228. </para>
  229. <para>
  230. An sample usage follows:
  231. </para>
  232. <programlisting language="php"><![CDATA[
  233. function __autoload($class)
  234. {
  235. Zend_Loader::loadClass($class);
  236. }
  237. $cacheFile = dirname(__FILE__) . '/xmlrpc.cache';
  238. $server = new Zend_XmlRpc_Server();
  239. if (!Zend_XmlRpc_Server_Cache::get($cacheFile, $server)) {
  240. require_once 'My/Services/Glue.php';
  241. require_once 'My/Services/Paste.php';
  242. require_once 'My/Services/Tape.php';
  243. $server->setClass('My_Services_Glue', 'glue'); // glue. namespace
  244. $server->setClass('My_Services_Paste', 'paste'); // paste. namespace
  245. $server->setClass('My_Services_Tape', 'tape'); // tape. namespace
  246. Zend_XmlRpc_Server_Cache::save($cacheFile, $server);
  247. }
  248. echo $server->handle();
  249. ]]></programlisting>
  250. <para>
  251. The above example attempts to retrieve a server definition from
  252. xmlrpc.cache in the same directory as the script. If unsuccessful,
  253. it loads the service classes it needs, attaches them to the server
  254. instance, and then attempts to create a new cache file with the
  255. server definition.
  256. </para>
  257. </sect2>
  258. <sect2 id="zend.xmlrpc.server.use">
  259. <title>Usage Examples</title>
  260. <para>
  261. Below are several usage examples, showing the full spectrum of
  262. options available to developers. Usage examples will each build
  263. on the previous example provided.
  264. </para>
  265. <sect3 id="zend.xmlrpc.server.use.case1">
  266. <title>Basic Usage</title>
  267. <para>
  268. The example below attaches a function as a dispatchable XML-RPC
  269. method and handles incoming calls.
  270. </para>
  271. <programlisting language="php"><![CDATA[
  272. /**
  273. * Return the MD5 sum of a value
  274. *
  275. * @param string $value Value to md5sum
  276. * @return string MD5 sum of value
  277. */
  278. function md5Value($value)
  279. {
  280. return md5($value);
  281. }
  282. $server = new Zend_XmlRpc_Server();
  283. $server->addFunction('md5Value');
  284. echo $server->handle();
  285. ]]></programlisting>
  286. </sect3>
  287. <sect3 id="zend.xmlrpc.server.use.case2">
  288. <title>Attaching a class</title>
  289. <para>
  290. The example below illustrates attaching a class' public methods
  291. as dispatchable XML-RPC methods.
  292. </para>
  293. <programlisting language="php"><![CDATA[
  294. require_once 'Services/Comb.php';
  295. $server = new Zend_XmlRpc_Server();
  296. $server->setClass('Services_Comb');
  297. echo $server->handle();
  298. ]]></programlisting>
  299. </sect3>
  300. <sect3 id="zend.xmlrpc.server.use.case3">
  301. <title>Attaching several classes using namespaces</title>
  302. <para>
  303. The example below illustrates attaching several classes, each
  304. with their own namespace.
  305. </para>
  306. <programlisting language="php"><![CDATA[
  307. require_once 'Services/Comb.php';
  308. require_once 'Services/Brush.php';
  309. require_once 'Services/Pick.php';
  310. $server = new Zend_XmlRpc_Server();
  311. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  312. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  313. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  314. echo $server->handle();
  315. ]]></programlisting>
  316. </sect3>
  317. <sect3 id="zend.xmlrpc.server.use.case4">
  318. <title>Specifying exceptions to use as valid fault responses</title>
  319. <para>
  320. The example below allows any Services_Exception-derived class to
  321. report its code and message in the fault response.
  322. </para>
  323. <programlisting language="php"><![CDATA[
  324. require_once 'Services/Exception.php';
  325. require_once 'Services/Comb.php';
  326. require_once 'Services/Brush.php';
  327. require_once 'Services/Pick.php';
  328. // Allow Services_Exceptions to report as fault responses
  329. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  330. $server = new Zend_XmlRpc_Server();
  331. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  332. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  333. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  334. echo $server->handle();
  335. ]]></programlisting>
  336. </sect3>
  337. <sect3 id="zend.xmlrpc.server.use.case5">
  338. <title>Utilizing a custom request object</title>
  339. <para>
  340. The example below instantiates a custom request object and
  341. passes it to the server to handle.
  342. </para>
  343. <programlisting language="php"><![CDATA[
  344. require_once 'Services/Request.php';
  345. require_once 'Services/Exception.php';
  346. require_once 'Services/Comb.php';
  347. require_once 'Services/Brush.php';
  348. require_once 'Services/Pick.php';
  349. // Allow Services_Exceptions to report as fault responses
  350. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  351. $server = new Zend_XmlRpc_Server();
  352. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  353. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  354. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  355. // Create a request object
  356. $request = new Services_Request();
  357. echo $server->handle($request);
  358. ]]></programlisting>
  359. </sect3>
  360. <sect3 id="zend.xmlrpc.server.use.case6">
  361. <title>Utilizing a custom response object</title>
  362. <para>
  363. The example below illustrates specifying a custom response class
  364. for the returned response.
  365. </para>
  366. <programlisting language="php"><![CDATA[
  367. require_once 'Services/Request.php';
  368. require_once 'Services/Response.php';
  369. require_once 'Services/Exception.php';
  370. require_once 'Services/Comb.php';
  371. require_once 'Services/Brush.php';
  372. require_once 'Services/Pick.php';
  373. // Allow Services_Exceptions to report as fault responses
  374. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  375. $server = new Zend_XmlRpc_Server();
  376. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  377. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  378. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  379. // Create a request object
  380. $request = new Services_Request();
  381. // Utilize a custom response
  382. $server->setResponseClass('Services_Response');
  383. echo $server->handle($request);
  384. ]]></programlisting>
  385. </sect3>
  386. <sect3 id="zend.xmlrpc.server.use.case7">
  387. <title>Cache server definitions between requests</title>
  388. <para>
  389. The example below illustrates caching server definitions
  390. between requests.
  391. </para>
  392. <programlisting language="php"><![CDATA[
  393. // Specify a cache file
  394. $cacheFile = dirname(__FILE__) . '/xmlrpc.cache';
  395. // Allow Services_Exceptions to report as fault responses
  396. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  397. $server = new Zend_XmlRpc_Server();
  398. // Attempt to retrieve server definition from cache
  399. if (!Zend_XmlRpc_Server_Cache::get($cacheFile, $server)) {
  400. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  401. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  402. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  403. // Save cache
  404. Zend_XmlRpc_Server_Cache::save($cacheFile, $server);
  405. }
  406. // Create a request object
  407. $request = new Services_Request();
  408. // Utilize a custom response
  409. $server->setResponseClass('Services_Response');
  410. echo $server->handle($request);
  411. ]]></programlisting>
  412. </sect3>
  413. </sect2>
  414. </sect1>
  415. <!--
  416. vim:se ts=4 sw=4 et:
  417. -->