Zend_XmlRpc_Server.xml 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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. <para>
  170. Allowing multiple signatures can lead to confusion for developers
  171. using the services; to keep things simple, a <acronym>XML-RPC</acronym>
  172. service method should only have a single signature.
  173. </para>
  174. </note>
  175. </sect2>
  176. <sect2 id="zend.xmlrpc.server.namespaces">
  177. <title>Utilizing Namespaces</title>
  178. <para>
  179. <acronym>XML-RPC</acronym> has a concept of namespacing; basically, it allows grouping
  180. <acronym>XML-RPC</acronym> methods by dot-delimited namespaces. This helps prevent
  181. naming collisions between methods served by different classes. As an
  182. example, the <acronym>XML-RPC</acronym> server is expected to server several methods in
  183. the 'system' namespace:
  184. </para>
  185. <itemizedlist>
  186. <listitem><para>system.listMethods</para></listitem>
  187. <listitem><para>system.methodHelp</para></listitem>
  188. <listitem><para>system.methodSignature</para></listitem>
  189. </itemizedlist>
  190. <para>
  191. Internally, these map to the methods of the same name in
  192. <classname>Zend_XmlRpc_Server</classname>.
  193. </para>
  194. <para>
  195. If you want to add namespaces to the methods you serve, simply
  196. provide a namespace to the appropriate method when attaching a
  197. function or class:
  198. </para>
  199. <programlisting language="php"><![CDATA[
  200. // All public methods in My_Service_Class will be accessible as
  201. // myservice.METHODNAME
  202. $server->setClass('My_Service_Class', 'myservice');
  203. // Function 'somefunc' will be accessible as funcs.somefunc
  204. $server->addFunction('somefunc', 'funcs');
  205. ]]></programlisting>
  206. </sect2>
  207. <sect2 id="zend.xmlrpc.server.request">
  208. <title>Custom Request Objects</title>
  209. <para>
  210. Most of the time, you'll simply use the default request type included with
  211. <classname>Zend_XmlRpc_Server</classname>,
  212. <classname>Zend_XmlRpc_Request_Http</classname>. However, there may be times when you
  213. need <acronym>XML-RPC</acronym> to be available via the <acronym>CLI</acronym>, a
  214. <acronym>GUI</acronym>, or other environment, or want to log incoming requests. To do
  215. so, you may create a custom request object that extends
  216. <classname>Zend_XmlRpc_Request</classname>. The most important thing to remember is to
  217. ensure that the <methodname>getMethod()</methodname> and
  218. <methodname>getParams()</methodname> methods are implemented so that the
  219. <acronym>XML-RPC</acronym> server can retrieve that information in order to dispatch the
  220. request.
  221. </para>
  222. </sect2>
  223. <sect2 id="zend.xmlrpc.server.response">
  224. <title>Custom Responses</title>
  225. <para>
  226. Similar to request objects, <classname>Zend_XmlRpc_Server</classname> can return custom
  227. response objects; by default, a <classname>Zend_XmlRpc_Response_Http</classname> object
  228. is returned, which sends an appropriate Content-Type <acronym>HTTP</acronym> header for
  229. use with <acronym>XML-RPC</acronym>. Possible uses of a custom object would be to log
  230. responses, or to send responses back to <constant>STDOUT</constant>.
  231. </para>
  232. <para>
  233. To use a custom response class, use
  234. <methodname>Zend_XmlRpc_Server::setResponseClass()</methodname> prior to calling
  235. <methodname>handle()</methodname>.
  236. </para>
  237. </sect2>
  238. <sect2 id="zend.xmlrpc.server.fault">
  239. <title>Handling Exceptions via Faults</title>
  240. <para>
  241. <classname>Zend_XmlRpc_Server</classname> catches Exceptions generated by a dispatched
  242. method, and generates an <acronym>XML-RPC</acronym> fault response when such an
  243. exception is caught. By default, however, the exception messages and
  244. codes are not used in a fault response. This is an intentional
  245. decision to protect your code; many exceptions expose more
  246. information about the code or environment than a developer would
  247. necessarily intend (a prime example includes database abstraction or
  248. access layer exceptions).
  249. </para>
  250. <para>
  251. Exception classes can be whitelisted to be used as fault responses,
  252. however. To do so, simply utilize
  253. <methodname>Zend_XmlRpc_Server_Fault::attachFaultException()</methodname> to pass an
  254. exception class to whitelist:
  255. </para>
  256. <programlisting language="php"><![CDATA[
  257. Zend_XmlRpc_Server_Fault::attachFaultException('My_Project_Exception');
  258. ]]></programlisting>
  259. <para>
  260. If you utilize an exception class that your other project exceptions
  261. inherit, you can then whitelist a whole family of exceptions at a
  262. time. <classname>Zend_XmlRpc_Server_Exception</classname>s are always whitelisted, to
  263. allow reporting specific internal errors (undefined methods, etc.).
  264. </para>
  265. <para>
  266. Any exception not specifically whitelisted will generate a fault
  267. response with a code of '404' and a message of 'Unknown error'.
  268. </para>
  269. </sect2>
  270. <sect2 id="zend.xmlrpc.server.caching">
  271. <title>Caching Server Definitions Between Requests</title>
  272. <para>
  273. Attaching many classes to an <acronym>XML-RPC</acronym> server instance can utilize a
  274. lot of resources; each class must introspect using the Reflection
  275. <acronym>API</acronym> (via <classname>Zend_Server_Reflection</classname>), which in
  276. turn generates a list of all possible method signatures to provide to the server class.
  277. </para>
  278. <para>
  279. To reduce this performance hit somewhat, <classname>Zend_XmlRpc_Server_Cache</classname>
  280. can be used to cache the server definition between requests. When
  281. combined with <methodname>__autoload()</methodname>, this can greatly increase
  282. performance.
  283. </para>
  284. <para>
  285. An sample usage follows:
  286. </para>
  287. <programlisting language="php"><![CDATA[
  288. function __autoload($class)
  289. {
  290. Zend_Loader::loadClass($class);
  291. }
  292. $cacheFile = dirname(__FILE__) . '/xmlrpc.cache';
  293. $server = new Zend_XmlRpc_Server();
  294. if (!Zend_XmlRpc_Server_Cache::get($cacheFile, $server)) {
  295. require_once 'My/Services/Glue.php';
  296. require_once 'My/Services/Paste.php';
  297. require_once 'My/Services/Tape.php';
  298. $server->setClass('My_Services_Glue', 'glue'); // glue. namespace
  299. $server->setClass('My_Services_Paste', 'paste'); // paste. namespace
  300. $server->setClass('My_Services_Tape', 'tape'); // tape. namespace
  301. Zend_XmlRpc_Server_Cache::save($cacheFile, $server);
  302. }
  303. echo $server->handle();
  304. ]]></programlisting>
  305. <para>
  306. The above example attempts to retrieve a server definition from
  307. <property>xmlrpc.cache</property> in the same directory as the script. If unsuccessful,
  308. it loads the service classes it needs, attaches them to the server
  309. instance, and then attempts to create a new cache file with the
  310. server definition.
  311. </para>
  312. </sect2>
  313. <sect2 id="zend.xmlrpc.server.use">
  314. <title>Usage Examples</title>
  315. <para>
  316. Below are several usage examples, showing the full spectrum of
  317. options available to developers. Usage examples will each build
  318. on the previous example provided.
  319. </para>
  320. <example id="zend.xmlrpc.server.use.attach-function">
  321. <title>Basic Usage</title>
  322. <para>
  323. The example below attaches a function as a dispatchable <acronym>XML-RPC</acronym>
  324. method and handles incoming calls.
  325. </para>
  326. <programlisting language="php"><![CDATA[
  327. /**
  328. * Return the MD5 sum of a value
  329. *
  330. * @param string $value Value to md5sum
  331. * @return string MD5 sum of value
  332. */
  333. function md5Value($value)
  334. {
  335. return md5($value);
  336. }
  337. $server = new Zend_XmlRpc_Server();
  338. $server->addFunction('md5Value');
  339. echo $server->handle();
  340. ]]></programlisting>
  341. </example>
  342. <example id="zend.xmlrpc.server.use.attach-class">
  343. <title>Attaching a class</title>
  344. <para>
  345. The example below illustrates attaching a class' public methods
  346. as dispatchable <acronym>XML-RPC</acronym> methods.
  347. </para>
  348. <programlisting language="php"><![CDATA[
  349. require_once 'Services/Comb.php';
  350. $server = new Zend_XmlRpc_Server();
  351. $server->setClass('Services_Comb');
  352. echo $server->handle();
  353. ]]></programlisting>
  354. </example>
  355. <example id="zend.xmlrpc.server.use.attach-class-with-arguments">
  356. <title>Attaching a class with arguments</title>
  357. <para>
  358. The following example illustrates how to attach a class' public
  359. methods and passing arguments to its methods. This can be used to specify certain
  360. defaults when registering service classes.
  361. </para>
  362. <programlisting language="php"><![CDATA[
  363. class Services_PricingService
  364. {
  365. /**
  366. * Calculate current price of product with $productId
  367. *
  368. * @param ProductRepository $productRepository
  369. * @param PurchaseRepository $purchaseRepository
  370. * @param integer $productId
  371. */
  372. public function calculate(ProductRepository $productRepository,
  373. PurchaseRepository $purchaseRepository,
  374. $productId)
  375. {
  376. ...
  377. }
  378. }
  379. $server = new Zend_XmlRpc_Server();
  380. $server->setClass('Services_PricingService',
  381. 'pricing',
  382. new ProductRepository(),
  383. new PurchaseRepository());
  384. ]]></programlisting>
  385. <para>
  386. The arguments passed at <methodname>setClass()</methodname> at server construction
  387. time are injected into the method call <command>pricing.calculate()</command> on
  388. remote invokation. In the example above, only the argument
  389. <varname>$purchaseId</varname> is expected from the client.
  390. </para>
  391. </example>
  392. <example id="zend.xmlrpc.server.use.attach-class-with-arguments-constructor">
  393. <title>Passing arguments only to constructor</title>
  394. <para>
  395. <classname>Zend_XmlRpc_Server</classname> allows to restrict argument passing to
  396. constructors only. This can be used for constructor dependency injection.
  397. To limit injection to constructors, call
  398. <methodname>sendArgumentsToAllMethods</methodname> and pass
  399. <constant>FALSE</constant> as an argument. This disables the default behavior of all
  400. arguments being injected into the remote method. In the example below the instance
  401. of <classname>ProductRepository</classname> and
  402. <classname>PurchaseRepository</classname> is only injected into the constructor of
  403. <classname>Services_PricingService2</classname>.
  404. </para>
  405. <programlisting language="php"><![CDATA[
  406. class Services_PricingService2
  407. {
  408. /**
  409. * @param ProductRepository $productRepository
  410. * @param PurchaseRepository $purchaseRepository
  411. */
  412. public function __construct(ProductRepository $productRepository,
  413. PurchaseRepository $purchaseRepository)
  414. {
  415. ...
  416. }
  417. /**
  418. * Calculate current price of product with $productId
  419. *
  420. * @param integer $productId
  421. * @return double
  422. */
  423. public function calculate($productId)
  424. {
  425. ...
  426. }
  427. }
  428. $server = new Zend_XmlRpc_Server();
  429. $server->sendArgumentsToAllMethods(false);
  430. $server->setClass('Services_PricingService2',
  431. 'pricing',
  432. new ProductRepository(),
  433. new PurchaseRepository());
  434. ]]></programlisting>
  435. </example>
  436. <example id="zend.xmlrpc.server.use.attach-instance">
  437. <title>Attaching a class instance</title>
  438. <para>
  439. <methodname>setClass()</methodname> allows to register a previously instantiated
  440. object at the server. Just pass an instance instead of the class name. Obviously
  441. passing arguments to the constructor is not possible with pre-instantiated
  442. objects.
  443. </para>
  444. </example>
  445. <example id="zend.xmlrpc.server.use.attach-several-classes-namespaces">
  446. <title>Attaching several classes using namespaces</title>
  447. <para>
  448. The example below illustrates attaching several classes, each
  449. with their own namespace.
  450. </para>
  451. <programlisting language="php"><![CDATA[
  452. require_once 'Services/Comb.php';
  453. require_once 'Services/Brush.php';
  454. require_once 'Services/Pick.php';
  455. $server = new Zend_XmlRpc_Server();
  456. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  457. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  458. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  459. echo $server->handle();
  460. ]]></programlisting>
  461. </example>
  462. <example id="zend.xmlrpc.server.use.exceptions-faults">
  463. <title>Specifying exceptions to use as valid fault responses</title>
  464. <para>
  465. The example below allows any <classname>Services_Exception</classname>-derived
  466. class to report its code and message in the fault response.
  467. </para>
  468. <programlisting language="php"><![CDATA[
  469. require_once 'Services/Exception.php';
  470. require_once 'Services/Comb.php';
  471. require_once 'Services/Brush.php';
  472. require_once 'Services/Pick.php';
  473. // Allow Services_Exceptions to report as fault responses
  474. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  475. $server = new Zend_XmlRpc_Server();
  476. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  477. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  478. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  479. echo $server->handle();
  480. ]]></programlisting>
  481. </example>
  482. <example id="zend.xmlrpc.server.use.custom-request-object">
  483. <title>Utilizing custom request and response objects</title>
  484. <para>
  485. Some use cases require to utilize a custom request object.
  486. For example, <acronym>XML/RPC</acronym> is not bound to
  487. <acronym>HTTP</acronym> as a transfer protocol. It is possible to use
  488. other transfer protocols like <acronym>SSH</acronym> or telnet to send
  489. the request and response data over the wire. Another use case is
  490. authentication and authorization. In case of a different transfer
  491. protocol, one need to change the implementation to read request data.
  492. </para>
  493. <para>
  494. The example below instantiates a custom request object and
  495. passes it to the server to handle.
  496. </para>
  497. <programlisting language="php"><![CDATA[
  498. require_once 'Services/Request.php';
  499. require_once 'Services/Exception.php';
  500. require_once 'Services/Comb.php';
  501. require_once 'Services/Brush.php';
  502. require_once 'Services/Pick.php';
  503. // Allow Services_Exceptions to report as fault responses
  504. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  505. $server = new Zend_XmlRpc_Server();
  506. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  507. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  508. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  509. // Create a request object
  510. $request = new Services_Request();
  511. echo $server->handle($request);
  512. ]]></programlisting>
  513. </example>
  514. <example id="zend.xmlrpc.server.use.custom-response-object">
  515. <title>Specifying a custom response class</title>
  516. <para>
  517. The example below illustrates specifying a custom response class
  518. for the returned response.
  519. </para>
  520. <programlisting language="php"><![CDATA[
  521. require_once 'Services/Request.php';
  522. require_once 'Services/Response.php';
  523. require_once 'Services/Exception.php';
  524. require_once 'Services/Comb.php';
  525. require_once 'Services/Brush.php';
  526. require_once 'Services/Pick.php';
  527. // Allow Services_Exceptions to report as fault responses
  528. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  529. $server = new Zend_XmlRpc_Server();
  530. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  531. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  532. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  533. // Create a request object
  534. $request = new Services_Request();
  535. // Utilize a custom response
  536. $server->setResponseClass('Services_Response');
  537. echo $server->handle($request);
  538. ]]></programlisting>
  539. </example>
  540. </sect2>
  541. <sect2 id="zend.xmlrpc.server.performance">
  542. <title>Performance optimization</title>
  543. <example id="zend.xmlrpc.server.performance.caching">
  544. <title>Cache server definitions between requests</title>
  545. <para>
  546. The example below illustrates caching server definitions between requests.
  547. </para>
  548. <programlisting language="php"><![CDATA[
  549. // Specify a cache file
  550. $cacheFile = dirname(__FILE__) . '/xmlrpc.cache';
  551. // Allow Services_Exceptions to report as fault responses
  552. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  553. $server = new Zend_XmlRpc_Server();
  554. // Attempt to retrieve server definition from cache
  555. if (!Zend_XmlRpc_Server_Cache::get($cacheFile, $server)) {
  556. $server->setClass('Services_Comb', 'comb'); // methods called as comb.*
  557. $server->setClass('Services_Brush', 'brush'); // methods called as brush.*
  558. $server->setClass('Services_Pick', 'pick'); // methods called as pick.*
  559. // Save cache
  560. Zend_XmlRpc_Server_Cache::save($cacheFile, $server);
  561. }
  562. // Create a request object
  563. $request = new Services_Request();
  564. // Utilize a custom response
  565. $server->setResponseClass('Services_Response');
  566. echo $server->handle($request);
  567. ]]></programlisting>
  568. </example>
  569. <note>
  570. <para>
  571. The server cache file should be located outside the document root.
  572. </para>
  573. </note>
  574. <example id="zend.xmlrpc.server.performance.xmlgen">
  575. <title>Optimizing XML generation</title>
  576. <para>
  577. <classname>Zend_XmlRpc_Server</classname> uses
  578. <classname>DOMDocument</classname> of <acronym>PHP</acronym>
  579. extension <emphasis>ext/dom</emphasis> to generate it's
  580. <acronym>XML</acronym> output. While <emphasis>ext/dom</emphasis> is
  581. available on a lot of hosts it is not exactly the fastest.
  582. Benchmarks have shown, that <classname>XmlWriter</classname>
  583. from <emphasis>ext/xmlwriter</emphasis> performs better.
  584. </para>
  585. <para>
  586. If <emphasis>ext/xmlwriter</emphasis> is available on your host, you can
  587. select a the <classname>XmlWriter</classname>-based generator
  588. to leaverage the performance differences.
  589. </para>
  590. <programlisting language="php"><![CDATA[
  591. require_once 'Zend/XmlRpc/Server.php';
  592. require_once 'Zend/XmlRpc/Generator/XmlWriter.php';
  593. Zend_XmlRpc_Value::setGenerator(new Zend_XmlRpc_Generator_XmlWriter());
  594. $server = new Zend_XmlRpc_Server();
  595. ...
  596. ]]></programlisting>
  597. </example>
  598. <note>
  599. <title>Benchmark your application</title>
  600. <para>
  601. Performance is determined by a lot of parameters and
  602. benchmarks only apply for the specific test case. Differences
  603. come from <acronym>PHP</acronym> version, installed extensions, webserver and
  604. operating system just to name a few. Please make sure to
  605. benchmark your application on your own and decide which
  606. generator to use based on <emphasis>your</emphasis> numbers.
  607. </para>
  608. </note>
  609. <note>
  610. <title>Benchmark your client</title>
  611. <para>
  612. This optimization makes sense for the client side too. Just
  613. select the alternate <acronym>XML</acronym> generator before
  614. doing any work with <classname>Zend_XmlRpc_Client</classname>.
  615. </para>
  616. </note>
  617. </sect2>
  618. </sect1>
  619. <!--
  620. vim:se ts=4 sw=4 et:
  621. -->