Zend_XmlRpc_Server.xml 18 KB

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