Zend_XmlRpc_Server.xml 19 KB

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