Zend_Soap_Server.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.soap.server">
  4. <title>Zend_Soap_Server</title>
  5. <para>
  6. <classname>Zend_Soap_Server</classname> class is intended to simplify Web Services server part development for <acronym>PHP</acronym> programmers.
  7. </para>
  8. <para>
  9. It may be used in WSDL or non-WSDL mode, and using classes or functions to define Web Service <acronym>API</acronym>.
  10. </para>
  11. <para>
  12. When <classname>Zend_Soap_Server</classname> component works in the WSDL mode, it uses already prepared WSDL document to define
  13. server object behavior and transport layer options.
  14. </para>
  15. <para>
  16. WSDL document may be auto-generated with functionality provided by
  17. <link linkend="zend.soap.autodiscovery.introduction">Zend_Soap_AutoDiscovery component</link> or should be constructed manually using
  18. <link linkend="zend.soap.wsdl"><classname>Zend_Soap_Wsdl</classname> class</link> or any other <acronym>XML</acronym> generating tool.
  19. </para>
  20. <para>
  21. If the non-WSDL mode is used, then all protocol options have to be set using options mechanism.
  22. </para>
  23. <sect2 id="zend.soap.server.constructor">
  24. <title>Zend_Soap_Server constructor</title>
  25. <para>
  26. <classname>Zend_Soap_Server</classname> constructor should be used a bit differently for WSDL and non-WSDL modes.
  27. </para>
  28. <sect3 id="zend.soap.server.constructor.wsdl_mode">
  29. <title>Zend_Soap_Server constructor for the WSDL mode</title>
  30. <para>
  31. <classname>Zend_Soap_Server</classname> constructor takes two optional parameters when it works in WSDL mode:
  32. <orderedlist>
  33. <listitem>
  34. <para>
  35. <varname>$wsdl</varname>, which is an <acronym>URI</acronym> of a WSDL file<footnote>
  36. <para>
  37. May be set later using <methodname>setWsdl($wsdl)</methodname> method.
  38. </para>
  39. </footnote>.
  40. </para>
  41. </listitem>
  42. <listitem>
  43. <para>
  44. <varname>$options</varname> - options to create <acronym>SOAP</acronym> server object<footnote>
  45. <para>
  46. Options may be set later using
  47. <methodname>setOptions($options)</methodname> method.
  48. </para>
  49. </footnote>.
  50. </para>
  51. <para>
  52. The following options are recognized in the WSDL mode:
  53. <itemizedlist>
  54. <listitem>
  55. <para>
  56. 'soap_version' ('soapVersion') - soap version to use (SOAP_1_1 or <acronym>SOAP</acronym>_1_2).
  57. </para>
  58. </listitem>
  59. <listitem>
  60. <para>
  61. 'actor' - the actor <acronym>URI</acronym> for the server.
  62. </para>
  63. </listitem>
  64. <listitem>
  65. <para>
  66. 'classmap' ('classMap') which can be used to map some WSDL types to <acronym>PHP</acronym> classes.
  67. </para>
  68. <para>
  69. The option must be an array with WSDL types as keys and names of <acronym>PHP</acronym> classes as values.
  70. </para>
  71. </listitem>
  72. <listitem>
  73. <para>
  74. 'encoding' - internal character encoding (UTF-8 is always used as an external encoding).
  75. </para>
  76. </listitem>
  77. <listitem>
  78. <para>
  79. 'wsdl' which is equivalent to <methodname>setWsdl($wsdlValue)</methodname> call.
  80. </para>
  81. </listitem>
  82. </itemizedlist>
  83. </para>
  84. </listitem>
  85. </orderedlist>
  86. </para>
  87. </sect3>
  88. <sect3 id="zend.soap.server.wsdl_mode">
  89. <title>Zend_Soap_Server constructor for the non-WSDL mode</title>
  90. <para>
  91. The first constructor parameter <emphasis>must</emphasis> be set to <constant>NULL</constant> if you
  92. plan to use <classname>Zend_Soap_Server</classname> functionality in non-WSDL mode.
  93. </para>
  94. <para>
  95. You also have to set 'uri' option in this case (see below).
  96. </para>
  97. <para>
  98. The second constructor parameter (<varname>$options</varname>) is an array with options to create
  99. <acronym>SOAP</acronym> server object<footnote>
  100. <para>
  101. Options may be set later using <methodname>setOptions($options)</methodname> method.
  102. </para>
  103. </footnote>.
  104. </para>
  105. <para>
  106. The following options are recognized in the non-WSDL mode:
  107. <itemizedlist>
  108. <listitem>
  109. <para>
  110. 'soap_version' ('soapVersion') - soap version to use (SOAP_1_1 or <acronym>SOAP</acronym>_1_2).
  111. </para>
  112. </listitem>
  113. <listitem>
  114. <para>
  115. 'actor' - the actor <acronym>URI</acronym> for the server.
  116. </para>
  117. </listitem>
  118. <listitem>
  119. <para>
  120. 'classmap' ('classMap') which can be used to map some WSDL types to <acronym>PHP</acronym> classes.
  121. </para>
  122. <para>
  123. The option must be an array with WSDL types as keys and names of <acronym>PHP</acronym> classes as values.
  124. </para>
  125. </listitem>
  126. <listitem>
  127. <para>
  128. 'encoding' - internal character encoding (UTF-8 is always used as an external encoding).
  129. </para>
  130. </listitem>
  131. <listitem>
  132. <para>
  133. 'uri' (required) - <acronym>URI</acronym> namespace for <acronym>SOAP</acronym> server.
  134. </para>
  135. </listitem>
  136. </itemizedlist>
  137. </para>
  138. </sect3>
  139. </sect2>
  140. <sect2 id="zend.soap.server.api_define_methods">
  141. <title>Methods to define Web Service API</title>
  142. <para>
  143. There are two ways to define Web Service <acronym>API</acronym> when your want to give access to your <acronym>PHP</acronym> code through <acronym>SOAP</acronym>.
  144. </para>
  145. <para>
  146. The first one is to attach some class to the <classname>Zend_Soap_Server</classname> object which has to completely describe
  147. Web Service API:
  148. <programlisting language="php"><![CDATA[
  149. ...
  150. class MyClass {
  151. /**
  152. * This method takes ...
  153. *
  154. * @param integer $inputParam
  155. * @return string
  156. */
  157. public function method1($inputParam) {
  158. ...
  159. }
  160. /**
  161. * This method takes ...
  162. *
  163. * @param integer $inputParam1
  164. * @param string $inputParam2
  165. * @return float
  166. */
  167. public function method2($inputParam1, $inputParam2) {
  168. ...
  169. }
  170. ...
  171. }
  172. ...
  173. $server = new Zend_Soap_Server(null, $options);
  174. // Bind Class to Soap Server
  175. $server->setClass('MyClass');
  176. // Bind already initialized object to Soap Server
  177. $server->setObject(new MyClass());
  178. ...
  179. $server->handle();
  180. ]]></programlisting>
  181. <note>
  182. <title>Important!</title>
  183. <para>
  184. You should completely describe each method using method docblock if you plan to use autodiscover functionality
  185. to prepare corresponding Web Service WSDL.
  186. </para>
  187. </note>
  188. </para>
  189. <para>
  190. The second method of defining Web Service API is using set of functions and <methodname>addFunction()</methodname> or
  191. <methodname>loadFunctions()</methodname> methods:
  192. <programlisting language="php"><![CDATA[
  193. ...
  194. /**
  195. * This function ...
  196. *
  197. * @param integer $inputParam
  198. * @return string
  199. */
  200. function function1($inputParam) {
  201. ...
  202. }
  203. /**
  204. * This function ...
  205. *
  206. * @param integer $inputParam1
  207. * @param string $inputParam2
  208. * @return float
  209. */
  210. function function2($inputParam1, $inputParam2) {
  211. ...
  212. }
  213. ...
  214. $server = new Zend_Soap_Server(null, $options);
  215. $server->addFunction('function1');
  216. $server->addFunction('function2');
  217. ...
  218. $server->handle();
  219. ]]></programlisting>
  220. </para>
  221. </sect2>
  222. <sect2 id="zend.soap.server.request_response">
  223. <title>Request and response objects handling</title>
  224. <note>
  225. <title>Advanced</title>
  226. <para>
  227. This section describes advanced request/response processing options and may be skipped.
  228. </para>
  229. </note>
  230. <para>
  231. <classname>Zend_Soap_Server</classname> component performs request/response processing automatically,
  232. but allows to catch it and do some pre- and post-processing.
  233. </para>
  234. <sect3 id="zend.soap.server.request_response.request">
  235. <title>Request processing</title>
  236. <para>
  237. <methodname>Zend_Soap_Server::handle()</methodname> method takes request from the standard input stream ('php://input').
  238. It may be overridden either by supplying optional parameter to the <methodname>handle()</methodname> method or
  239. by setting request using <methodname>setRequest()</methodname> method:
  240. <programlisting language="php"><![CDATA[
  241. ...
  242. $server = new Zend_Soap_Server(...);
  243. ...
  244. // Set request using optional $request parameter
  245. $server->handle($request);
  246. ...
  247. // Set request using setRequest() method
  248. $server->setRequest();
  249. $server->handle();
  250. ]]></programlisting>
  251. </para>
  252. <para>
  253. Request object may be represented using any of the following:
  254. <itemizedlist>
  255. <listitem>
  256. <para>
  257. DOMDocument (casted to <acronym>XML</acronym>)
  258. </para>
  259. </listitem>
  260. <listitem>
  261. <para>
  262. DOMNode (owner document is grabbed and casted to <acronym>XML</acronym>)
  263. </para>
  264. </listitem>
  265. <listitem>
  266. <para>
  267. SimpleXMLElement (casted to <acronym>XML</acronym>)
  268. </para>
  269. </listitem>
  270. <listitem>
  271. <para>
  272. stdClass (__toString() is called and verified to be valid <acronym>XML</acronym>)
  273. </para>
  274. </listitem>
  275. <listitem>
  276. <para>
  277. string (verified to be valid <acronym>XML</acronym>)
  278. </para>
  279. </listitem>
  280. </itemizedlist>
  281. </para>
  282. <para>
  283. Last processed request may be retrieved using <methodname>getLastRequest()</methodname> method as an XML string:
  284. <programlisting language="php"><![CDATA[
  285. ...
  286. $server = new Zend_Soap_Server(...);
  287. ...
  288. $server->handle();
  289. $request = $server->getLastRequest();
  290. ]]></programlisting>
  291. </para>
  292. </sect3>
  293. <sect3 id="zend.soap.server.request_response.response">
  294. <title>Response pre-processing</title>
  295. <para>
  296. <methodname>Zend_Soap_Server::handle()</methodname> method automatically emits generated response to the output stream.
  297. It may be blocked using <methodname>setReturnResponse()</methodname> with <constant>TRUE</constant> or <constant>FALSE</constant>
  298. as a parameter<footnote>
  299. <para>
  300. Current state of the Return Response flag may be requested with
  301. <methodname>setReturnResponse()</methodname> method.
  302. </para>
  303. </footnote>.
  304. Generated response is returned by <methodname>handle()</methodname> method in this case.
  305. <programlisting language="php"><![CDATA[
  306. ...
  307. $server = new Zend_Soap_Server(...);
  308. ...
  309. // Get a response as a return value of handle() method
  310. // instead of emitting it to the standard output
  311. $server->setReturnResponse(true);
  312. ...
  313. $response = $server->handle();
  314. ...
  315. ]]></programlisting>
  316. </para>
  317. <para>
  318. Last response may be also retrieved by <methodname>getLastResponse()</methodname> method for some post-processing:
  319. <programlisting language="php"><![CDATA[
  320. ...
  321. $server = new Zend_Soap_Server(...);
  322. ...
  323. $server->handle();
  324. $response = $server->getLastResponse();
  325. ...
  326. ]]></programlisting>
  327. </para>
  328. </sect3>
  329. </sect2>
  330. </sect1>