Zend_XmlRpc_Server.xml 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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.anatomy">
  55. <title>Anatomy of a webservice</title>
  56. <sect3 id="zend.xmlrpc.server.anatomy.general">
  57. <title>General considerations</title>
  58. <para>
  59. For maximum performance it is recommended to use a simple
  60. bootstrap file for the server component. Using
  61. <classname>Zend_XmlRpc_Server</classname> inside a
  62. <link linkend="zend.controller"><classname>Zend_Controller</classname></link>
  63. is strongly discouraged to avoid the overhead.
  64. </para>
  65. <para>
  66. Services change over time and while webservices are generally
  67. less change intense as code-native <acronym>APIs</acronym>, it
  68. is recommended to version your service. Do so to lay grounds to
  69. provide compatibility for clients using older versions of your
  70. service and manage your service lifecycle including deprecation
  71. timeframes.To do so just include a version number into your
  72. <acronym>URI</acronym>. It is also recommended to include the
  73. remote protocol name in the <acronym>URI</acronym> to allow easy
  74. integration of upcoming remoting technologies.
  75. http://myservice.ws/<emphasis>1.0/XMLRPC/</emphasis>.
  76. </para>
  77. </sect3>
  78. <sect3 id="zend.xmlrpc.server.anatomy.expose">
  79. <title>What to expose?</title>
  80. <para>
  81. Most of the time it is not sensible to expose business objects
  82. directly. Business objects are usually small and under heavy
  83. change, because change is cheap in this layer of your
  84. application. Once deployed and adopted, web services are hard to
  85. change. Another concern is <acronym>I/O</acronym> and latency:
  86. the best webservice calls are those not happening. Therefore
  87. service calls need to be more coarse-grained than usual business
  88. logic is. Often an additional layer in front of your business
  89. objects makes sense. This layer is sometimes referred to as <ulink
  90. url="http://martinfowler.com/eaaCatalog/remoteFacade.html">Remote
  91. Facade</ulink>.
  92. Such a service layer adds a coarse grained interface on top of
  93. your business logic and groups verbose operations into smaller
  94. ones.
  95. </para>
  96. </sect3>
  97. </sect2>
  98. <sect2 id="zend.xmlrpc.server.conventions">
  99. <title>Conventions</title>
  100. <para>
  101. <classname>Zend_XmlRpc_Server</classname> allows the developer to attach functions and
  102. class method calls as dispatchable <acronym>XML-RPC</acronym> methods. Via
  103. <classname>Zend_Server_Reflection</classname>, it does introspection on all attached
  104. methods, using the function and method docblocks to determine the
  105. method help text and method signatures.
  106. </para>
  107. <para>
  108. <acronym>XML-RPC</acronym> types do not necessarily map one-to-one to
  109. <acronym>PHP</acronym> types. However, the code will do its best to guess the
  110. appropriate type based on the values listed in @param and @return lines. Some
  111. <acronym>XML-RPC</acronym> types have no immediate <acronym>PHP</acronym> equivalent,
  112. however, and should be hinted using the <acronym>XML-RPC</acronym> type in the PHPDoc.
  113. These include:
  114. </para>
  115. <itemizedlist>
  116. <listitem>
  117. <para>
  118. <emphasis><property>dateTime.iso8601</property></emphasis>, a string formatted
  119. as '<command>YYYYMMDDTHH:mm:ss</command>'
  120. </para>
  121. </listitem>
  122. <listitem><para><emphasis>base64</emphasis>, base64 encoded data</para></listitem>
  123. <listitem><para><emphasis>struct</emphasis>, any associative array</para></listitem>
  124. </itemizedlist>
  125. <para>
  126. An example of how to hint follows:
  127. </para>
  128. <programlisting language="php"><![CDATA[
  129. /**
  130. * This is a sample function
  131. *
  132. * @param base64 $val1 Base64-encoded data
  133. * @param dateTime.iso8601 $val2 An ISO date
  134. * @param struct $val3 An associative array
  135. * @return struct
  136. */
  137. function myFunc($val1, $val2, $val3)
  138. {
  139. }
  140. ]]></programlisting>
  141. <para>
  142. PhpDocumentor does no validation of the types specified for params
  143. or return values, so this will have no impact on your <acronym>API</acronym>
  144. documentation. Providing the hinting is necessary, however, when the
  145. server is validating the parameters provided to the method call.
  146. </para>
  147. <para>
  148. It is perfectly valid to specify multiple types for both params and
  149. return values; the <acronym>XML-RPC</acronym> specification even suggests that
  150. system.methodSignature should return an array of all possible method
  151. signatures (i.e., all possible combinations of param and return
  152. values). You may do so just as you normally would with
  153. PhpDocumentor, using the '|' operator:
  154. </para>
  155. <programlisting language="php"><![CDATA[
  156. /**
  157. * This is a sample function
  158. *
  159. * @param string|base64 $val1 String or base64-encoded data
  160. * @param string|dateTime.iso8601 $val2 String or an ISO date
  161. * @param array|struct $val3 Normal indexed array or an associative array
  162. * @return boolean|struct
  163. */
  164. function myFunc($val1, $val2, $val3)
  165. {
  166. }
  167. ]]></programlisting>
  168. <note>
  169. Allowing multiple signatures can lead to confusion for developers
  170. using the services; to keep things simple, a <acronym>XML-RPC</acronym>
  171. service method should only have a single signature.
  172. </note>
  173. </sect2>
  174. <sect2 id="zend.xmlrpc.server.namespaces">
  175. <title>Utilizing Namespaces</title>
  176. <para>
  177. <acronym>XML-RPC</acronym> has a concept of namespacing; basically, it allows grouping
  178. <acronym>XML-RPC</acronym> methods by dot-delimited namespaces. This helps prevent
  179. naming collisions between methods served by different classes. As an
  180. example, the <acronym>XML-RPC</acronym> server is expected to server several methods in
  181. the 'system' namespace:
  182. </para>
  183. <itemizedlist>
  184. <listitem><para>system.listMethods</para></listitem>
  185. <listitem><para>system.methodHelp</para></listitem>
  186. <listitem><para>system.methodSignature</para></listitem>
  187. </itemizedlist>
  188. <para>
  189. Internally, these map to the methods of the same name in
  190. <classname>Zend_XmlRpc_Server</classname>.
  191. </para>
  192. <para>
  193. If you want to add namespaces to the methods you serve, simply
  194. provide a namespace to the appropriate method when attaching a
  195. function or class:
  196. </para>
  197. <programlisting language="php"><![CDATA[
  198. // All public methods in My_Service_Class will be accessible as
  199. // myservice.METHODNAME
  200. $server->setClass('My_Service_Class', 'myservice');
  201. // Function 'somefunc' will be accessible as funcs.somefunc
  202. $server->addFunction('somefunc', 'funcs');
  203. ]]></programlisting>
  204. </sect2>
  205. <sect2 id="zend.xmlrpc.server.request">
  206. <title>Custom Request Objects</title>
  207. <para>
  208. Most of the time, you'll simply use the default request type included with
  209. <classname>Zend_XmlRpc_Server</classname>,
  210. <classname>Zend_XmlRpc_Request_Http</classname>. However, there may be times when you
  211. need <acronym>XML-RPC</acronym> to be available via the <acronym>CLI</acronym>, a
  212. <acronym>GUI</acronym>, or other environment, or want to log incoming requests. To do
  213. so, you may create a custom request object that extends
  214. <classname>Zend_XmlRpc_Request</classname>. The most important thing to remember is to
  215. ensure that the <methodname>getMethod()</methodname> and
  216. <methodname>getParams()</methodname> methods are implemented so that the
  217. <acronym>XML-RPC</acronym> server can retrieve that information in order to dispatch the
  218. request.
  219. </para>
  220. </sect2>
  221. <sect2 id="zend.xmlrpc.server.response">
  222. <title>Custom Responses</title>
  223. <para>
  224. Similar to request objects, <classname>Zend_XmlRpc_Server</classname> can return custom
  225. response objects; by default, a <classname>Zend_XmlRpc_Response_Http</classname> object
  226. is returned, which sends an appropriate Content-Type <acronym>HTTP</acronym> header for
  227. use with <acronym>XML-RPC</acronym>. Possible uses of a custom object would be to log
  228. responses, or to send responses back to <constant>STDOUT</constant>.
  229. </para>
  230. <para>
  231. To use a custom response class, use
  232. <methodname>Zend_XmlRpc_Server::setResponseClass()</methodname> prior to calling
  233. <methodname>handle()</methodname>.
  234. </para>
  235. </sect2>
  236. <sect2 id="zend.xmlrpc.server.fault">
  237. <title>Handling Exceptions via Faults</title>
  238. <para>
  239. <classname>Zend_XmlRpc_Server</classname> catches Exceptions generated by a dispatched
  240. method, and generates an <acronym>XML-RPC</acronym> fault response when such an
  241. exception is caught. By default, however, the exception messages and
  242. codes are not used in a fault response. This is an intentional
  243. decision to protect your code; many exceptions expose more
  244. information about the code or environment than a developer would
  245. necessarily intend (a prime example includes database abstraction or
  246. access layer exceptions).
  247. </para>
  248. <para>
  249. Exception classes can be whitelisted to be used as fault responses,
  250. however. To do so, simply utilize
  251. <methodname>Zend_XmlRpc_Server_Fault::attachFaultException()</methodname> to pass an
  252. exception class to whitelist:
  253. </para>
  254. <programlisting language="php"><![CDATA[
  255. Zend_XmlRpc_Server_Fault::attachFaultException('My_Project_Exception');
  256. ]]></programlisting>
  257. <para>
  258. If you utilize an exception class that your other project exceptions
  259. inherit, you can then whitelist a whole family of exceptions at a
  260. time. <classname>Zend_XmlRpc_Server_Exception</classname>s are always whitelisted, to
  261. allow reporting specific internal errors (undefined methods, etc.).
  262. </para>
  263. <para>
  264. Any exception not specifically whitelisted will generate a fault
  265. response with a code of '404' and a message of 'Unknown error'.
  266. </para>
  267. </sect2>
  268. <sect2 id="zend.xmlrpc.server.caching">
  269. <title>Caching Server Definitions Between Requests</title>
  270. <para>
  271. Attaching many classes to an <acronym>XML-RPC</acronym> server instance can utilize a
  272. lot of resources; each class must introspect using the Reflection
  273. <acronym>API</acronym> (via <classname>Zend_Server_Reflection</classname>), which in
  274. turn generates a list of all possible method signatures to provide to the server class.
  275. </para>
  276. <para>
  277. To reduce this performance hit somewhat, <classname>Zend_XmlRpc_Server_Cache</classname>
  278. can be used to cache the server definition between requests. When
  279. combined with <methodname>__autoload()</methodname>, this can greatly increase
  280. performance.
  281. </para>
  282. <para>
  283. An sample usage follows:
  284. </para>
  285. <programlisting language="php"><![CDATA[
  286. function __autoload($class)
  287. {
  288. Zend_Loader::loadClass($class);
  289. }
  290. $cacheFile = dirname(__FILE__) . '/xmlrpc.cache';
  291. $server = new Zend_XmlRpc_Server();
  292. if (!Zend_XmlRpc_Server_Cache::get($cacheFile, $server)) {
  293. require_once 'My/Services/Glue.php';
  294. require_once 'My/Services/Paste.php';
  295. require_once 'My/Services/Tape.php';
  296. $server->setClass('My_Services_Glue', 'glue'); // glue. namespace
  297. $server->setClass('My_Services_Paste', 'paste'); // paste. namespace
  298. $server->setClass('My_Services_Tape', 'tape'); // tape. namespace
  299. Zend_XmlRpc_Server_Cache::save($cacheFile, $server);
  300. }
  301. echo $server->handle();
  302. ]]></programlisting>
  303. <para>
  304. The above example attempts to retrieve a server definition from
  305. <property>xmlrpc.cache</property> in the same directory as the script. If unsuccessful,
  306. it loads the service classes it needs, attaches them to the server
  307. instance, and then attempts to create a new cache file with the
  308. server definition.
  309. </para>
  310. </sect2>
  311. <sect2 id="zend.xmlrpc.server.use">
  312. <title>Usage Examples</title>
  313. <para>
  314. Below are several usage examples, showing the full spectrum of
  315. options available to developers. Usage examples will each build
  316. on the previous example provided.
  317. </para>
  318. <example id="zend.xmlrpc.server.use.attach-function">
  319. <title>Basic Usage</title>
  320. <para>
  321. The example below attaches a function as a dispatchable <acronym>XML-RPC</acronym>
  322. method and handles incoming calls.
  323. </para>
  324. <programlisting language="php"><![CDATA[
  325. /**
  326. * Return the MD5 sum of a value
  327. *
  328. * @param string $value Value to md5sum
  329. * @return string MD5 sum of value
  330. */
  331. function md5Value($value)
  332. {
  333. return md5($value);
  334. }
  335. $server = new Zend_XmlRpc_Server();
  336. $server->addFunction('md5Value');
  337. echo $server->handle();
  338. ]]></programlisting>
  339. </example>
  340. <example id="zend.xmlrpc.server.use.attach-class">
  341. <title>Attaching a class</title>
  342. <para>
  343. The example below illustrates attaching a class' public methods
  344. as dispatchable <acronym>XML-RPC</acronym> methods.
  345. </para>
  346. <programlisting language="php"><![CDATA[
  347. require_once 'Services/Comb.php';
  348. $server = new Zend_XmlRpc_Server();
  349. $server->setClass('Services_Comb');
  350. echo $server->handle();
  351. ]]></programlisting>
  352. </example>
  353. <example id="zend.xmlrpc.server.use.attach-class-with-arguments">
  354. <title>Attaching a class with arguments</title>
  355. <para>
  356. The following example illustrates how to attach a class' public
  357. methods and passing arguments to its methods. This can be used to specify certain
  358. defaults when registering service classes.
  359. </para>
  360. <programlisting language="php"><![CDATA[
  361. class Services_PricingService
  362. {
  363. /**
  364. * Calculate current price of product with $productId
  365. *
  366. * @param ProductRepository $productRepository
  367. * @param PurchaseRepository $purchaseRepository
  368. * @param integer $productId
  369. */
  370. public function calculate(ProductRepository $productRepository,
  371. PurchaseRepository $purchaseRepository,
  372. $productId)
  373. {
  374. ...
  375. }
  376. }
  377. $server = new Zend_XmlRpc_Server();
  378. $server->setClass('Services_PricingService', 'pricing', new ProductRepository(), new PurchaseRepository());
  379. ]]></programlisting>
  380. <para>
  381. The arguments passed at <methodname>setClass()</methodname> at server construction time are
  382. injected into the method call <command>pricing.calculate()</command> on remote invokation.
  383. In the example above, only the argument <code>$purchaseId</code> is expected from the client.
  384. </para>
  385. </example>
  386. <example id="zend.xmlrpc.server.use.attach-class-with-arguments-constructor">
  387. <title>Passing arguments only to constructor</title>
  388. <para>
  389. <classname>Zend_XmlRpc_Server</classname> allows to restrict argument passing to
  390. constructors only. This can be used for constructor dependency injection.
  391. To limit injection to constructors, call <methodname>sendArgumentsToAllMethods</methodname>
  392. and pass <code>false</code> as an argument. This disables the default behavior of all arguments
  393. being injected into the remote method. In the example below the instance of
  394. <classname>ProductRepository</classname> and <classname>PurchaseRepository</classname> is only
  395. injected into the constructor of <classname>Services_PricingService2</classname>.
  396. </para>
  397. <programlisting language="php"><![CDATA[
  398. class Services_PricingService2
  399. {
  400. /**
  401. * @param ProductRepository $productRepository
  402. * @param PurchaseRepository $purchaseRepository
  403. */
  404. public function __construct(ProductRepository $productRepository,
  405. PurchaseRepository $purchaseRepository)
  406. {
  407. ...
  408. }
  409. /**
  410. * Calculate current price of product with $productId
  411. *
  412. * @param integer $productId
  413. * @return double
  414. */
  415. public function calculate($productId)
  416. {
  417. ...
  418. }
  419. }
  420. $server = new Zend_XmlRpc_Server();
  421. $server->sendArgumentsToAllMethods(false);
  422. $server->setClass('Services_PricingService2', 'pricing', new ProductRepository(), new PurchaseRepository());
  423. ]]></programlisting>
  424. </example>
  425. <example id="zend.xmlrpc.server.use.attach-instance">
  426. <title>Attaching a class instance</title>
  427. <para>
  428. <methodname>setClass()</methodname> allows to register a previously instantiated
  429. object at the server. Just pass an instance instead of the class name. Obviously
  430. passing arguments to the constructor is not possible with pre-instantiated
  431. objects.
  432. </para>
  433. </example>
  434. <example id="zend.xmlrpc.server.use.attach-several-classes-namespaces">
  435. <title>Attaching several classes using namespaces</title>
  436. <para>
  437. The example below illustrates attaching several classes, each
  438. with their own namespace.
  439. </para>
  440. <programlisting language="php"><![CDATA[
  441. require_once 'Services/Comb.php';
  442. require_once 'Services/Brush.php';
  443. require_once 'Services/Pick.php';
  444. $server = new Zend_XmlRpc_Server();
  445. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  446. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  447. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  448. echo $server->handle();
  449. ]]></programlisting>
  450. </example>
  451. <example id="zend.xmlrpc.server.use.exceptions-faults">
  452. <title>Specifying exceptions to use as valid fault responses</title>
  453. <para>
  454. The example below allows any <classname>Services_Exception</classname>-derived
  455. class to report its code and message in the fault response.
  456. </para>
  457. <programlisting language="php"><![CDATA[
  458. require_once 'Services/Exception.php';
  459. require_once 'Services/Comb.php';
  460. require_once 'Services/Brush.php';
  461. require_once 'Services/Pick.php';
  462. // Allow Services_Exceptions to report as fault responses
  463. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  464. $server = new Zend_XmlRpc_Server();
  465. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  466. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  467. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  468. echo $server->handle();
  469. ]]></programlisting>
  470. </example>
  471. <example id="zend.xmlrpc.server.use.custom-request-object">
  472. <title>Utilizing custom request and response objects</title>
  473. <para>
  474. Some use cases require to utilize a custom request object.
  475. For example, <acronym>XML/RPC</acronym> is not bound to
  476. <acronym>HTTP</acronym> as a transfer protocol. It is possible to use
  477. other transfer protocols like <acronym>SSH</acronym> or telnet to send
  478. the request and response data over the wire. Another use case is
  479. authentication and authorization. In case of a different transfer
  480. protocol, one need to change the implementation to read request data.
  481. </para>
  482. <para>
  483. The example below instantiates a custom request object and
  484. passes it to the server to handle.
  485. </para>
  486. <programlisting language="php"><![CDATA[
  487. require_once 'Services/Request.php';
  488. require_once 'Services/Exception.php';
  489. require_once 'Services/Comb.php';
  490. require_once 'Services/Brush.php';
  491. require_once 'Services/Pick.php';
  492. // Allow Services_Exceptions to report as fault responses
  493. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  494. $server = new Zend_XmlRpc_Server();
  495. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  496. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  497. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  498. // Create a request object
  499. $request = new Services_Request();
  500. echo $server->handle($request);
  501. ]]></programlisting>
  502. </example>
  503. <example id="zend.xmlrpc.server.use.custom-response-object">
  504. <title>Specifying a custom response class</title>
  505. <para>
  506. The example below illustrates specifying a custom response class
  507. for the returned response.
  508. </para>
  509. <programlisting language="php"><![CDATA[
  510. require_once 'Services/Request.php';
  511. require_once 'Services/Response.php';
  512. require_once 'Services/Exception.php';
  513. require_once 'Services/Comb.php';
  514. require_once 'Services/Brush.php';
  515. require_once 'Services/Pick.php';
  516. // Allow Services_Exceptions to report as fault responses
  517. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  518. $server = new Zend_XmlRpc_Server();
  519. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  520. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  521. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  522. // Create a request object
  523. $request = new Services_Request();
  524. // Utilize a custom response
  525. $server->setResponseClass('Services_Response');
  526. echo $server->handle($request);
  527. ]]></programlisting>
  528. </example>
  529. </sect2>
  530. <sect2 id="zend.xmlrpc.server.performance">
  531. <title>Performance optimization</title>
  532. <example id="zend.xmlrpc.server.performance.caching">
  533. <title>Cache server definitions between requests</title>
  534. <para>
  535. The example below illustrates caching server definitions
  536. between requests.
  537. </para>
  538. <programlisting language="php"><![CDATA[
  539. // Specify a cache file
  540. $cacheFile = dirname(__FILE__) . '/xmlrpc.cache';
  541. // Allow Services_Exceptions to report as fault responses
  542. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  543. $server = new Zend_XmlRpc_Server();
  544. // Attempt to retrieve server definition from cache
  545. if (!Zend_XmlRpc_Server_Cache::get($cacheFile, $server)) {
  546. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  547. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  548. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  549. // Save cache
  550. Zend_XmlRpc_Server_Cache::save($cacheFile, $server);
  551. }
  552. // Create a request object
  553. $request = new Services_Request();
  554. // Utilize a custom response
  555. $server->setResponseClass('Services_Response');
  556. echo $server->handle($request);
  557. ]]></programlisting>
  558. <note>
  559. The server cache file should be located outside the document root.
  560. </note>
  561. </example>
  562. <example id="zend.xmlrpc.server.performance.xmlgen">
  563. <title>Optimizing XML generation</title>
  564. <para>
  565. <classname>Zend_XmlRpc_Server</classname> uses
  566. <classname>DOMDocument</classname> of <acronym>PHP</acronym>
  567. extension <code>ext/dom</code> to generate it's
  568. <acronym>XML</acronym> output. While <code>ext/dom</code> is
  569. available on a lot of hosts it is is not exactly the fastest.
  570. Benchmarks have shown, that <classname>XMLWriter</classname>
  571. from <code>ext/xmlwriter</code> performs better.
  572. </para>
  573. <para>
  574. If <code>ext/xmlwriter</code> is available on your host, you can
  575. select a the <classname>XMLWriter</classname>-based generator
  576. to leaverage the performance differences.
  577. </para>
  578. <programlisting language="php"><![CDATA[
  579. <?php
  580. require_once 'Zend/XmlRpc/Server.php';
  581. require_once 'Zend/XmlRpc/Generator/XMLWriter.php';
  582. Zend_XmlRpc_Value::setGenerator(new Zend_XmlRpc_Generator_XMLWriter());
  583. $server = new Zend_XmlRpc_Server();
  584. ...
  585. ]]></programlisting>
  586. <note>
  587. <title>Benchmark your application</title>
  588. <para>
  589. Performance is determined by a lot of parameters and
  590. benchmarks only apply for the specific test case. Differences
  591. come from PHP version, installed extensions, webserver and
  592. operating system just to name a few. Please make sure to
  593. benchmark your application on your own and decide which
  594. generator to use based on <emphasis>your</emphasis> numbers.
  595. </para>
  596. </note>
  597. <note>
  598. <title>Benchmark your client</title>
  599. <para>
  600. This optimization makes sense for the client side too. Just
  601. select the alternate <acronym>XML</acronym> generator before
  602. doing any work with <classname>Zend_XmlRpc_Client</classname>.
  603. </para>
  604. </note>
  605. </example>
  606. </sect2>
  607. </sect1>
  608. <!--
  609. vim:se ts=4 sw=4 et:
  610. -->