Zend_Soap_Server.xml 15 KB

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