Zend_Soap_AutoDiscovery.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.soap.autodiscovery">
  4. <title>AutoDiscovery</title>
  5. <sect2 id="zend.soap.autodiscovery.introduction">
  6. <title>AutoDiscovery Introduction</title>
  7. <para>
  8. SOAP functionality implemented within Zend Framework is intended to make all steps
  9. required for SOAP communications more simple.
  10. </para>
  11. <para>
  12. SOAP is language independent protocol. So it may be used not only for PHP-to-PHP communications.
  13. </para>
  14. <para>
  15. There are three configurations for SOAP applications where Zend Framework may be utilized:
  16. <orderedlist>
  17. <listitem>
  18. <simpara>SOAP server PHP application &lt;---&gt; SOAP client PHP application</simpara>
  19. </listitem>
  20. <listitem>
  21. <simpara>SOAP server non-PHP application &lt;---&gt; SOAP client PHP application</simpara>
  22. </listitem>
  23. <listitem>
  24. <simpara>SOAP server PHP application &lt;---&gt; SOAP client non-PHP application</simpara>
  25. </listitem>
  26. </orderedlist>
  27. </para>
  28. <para>
  29. We always have to know, which functionality is provided by SOAP server to operate with it.
  30. <ulink url="http://www.w3.org/TR/wsdl">WSDL</ulink> is used to describe network service API
  31. in details.
  32. </para>
  33. <para>
  34. WSDL language is complex enough (see <ulink url="http://www.w3.org/TR/wsdl">http://www.w3.org/TR/wsdl</ulink>
  35. for the details). So it's difficult to prepare correct WSDL description.
  36. </para>
  37. <para>
  38. Another problem is synchronizing changes in network service API with already existing WSDL.
  39. </para>
  40. <para>
  41. Both these problem may be solved by WSDL autogeneration. A prerequisite for this is a SOAP server autodiscovery.
  42. It constructs object similar to object used in SOAP server application, extracts necessary information and generates
  43. correct WSDL using this information.
  44. </para>
  45. <para>
  46. There are two ways for using Zend Framework for SOAP server application:
  47. <itemizedlist>
  48. <listitem>
  49. <para>Use separated class.</para>
  50. </listitem>
  51. <listitem>
  52. <para>Use set of functions</para>
  53. </listitem>
  54. </itemizedlist>
  55. </para>
  56. <para>
  57. Both methods are supported by Zend Framework Autodiscovery functionality.
  58. </para>
  59. <para>
  60. The<classname>Zend_Soap_AutoDiscover</classname> class also supports datatypes mapping from PHP to <ulink url="http://www.w3.org/TR/xmlschema-2/">XSD types</ulink>.
  61. </para>
  62. <para>
  63. Here is an example of common usage of the autodiscovery functionality. The <code>handle()</code>
  64. function generates the WSDL file and posts it to the browser.
  65. <programlisting language="php"><![CDATA[
  66. class My_SoapServer_Class {
  67. ...
  68. }
  69. $autodiscover = new Zend_Soap_AutoDiscover();
  70. $autodiscover->setClass('My_SoapServer_Class');
  71. $autodiscover->handle();
  72. ]]></programlisting>
  73. </para>
  74. <para>
  75. If you need access to the generated WSDL file either to save it to a file or
  76. as an XML string you can use the <code>dump($filename)</code> or <code>toXml()</code>
  77. functions the AutoDiscover class provides.
  78. </para>
  79. <note id="zend.soap.autodiscovery.introduction.noserver">
  80. <title>Zend_Soap_Autodiscover is not a Soap Server</title>
  81. <para>
  82. It is very important to note, that the class <classname>Zend_Soap_AutoDiscover</classname> does not act
  83. as a SOAP Server on its own. It only generates the WSDL and serves it to anyone accessing
  84. the url it is listening on.
  85. </para>
  86. <para>
  87. As the SOAP Endpoint Uri is uses the default
  88. <code>'http://' .$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']</code>,
  89. but this can be changed with the <code>setUri()</code> function or the Constructor
  90. parameter of <classname>Zend_Soap_AutoDiscover</classname> class. The endpoint has to provide
  91. a <classname>Zend_Soap_Server</classname> that listens to requests.
  92. </para>
  93. <para>
  94. <programlisting language="php"><![CDATA[
  95. if(isset($_GET['wsdl'])) {
  96. $autodiscover = new Zend_Soap_AutoDiscover();
  97. $autodiscover->setClass('HelloWorldService');
  98. $autodiscover->handle();
  99. } else {
  100. // pointing to the current file here
  101. $soap = new Zend_Soap_Server("http://example.com/soap.php?wsdl");
  102. $soap->setClass('HelloWorldService');
  103. $soap->handle();
  104. }
  105. ]]></programlisting>
  106. </para>
  107. </note>
  108. </sect2>
  109. <sect2 id="zend.soap.autodiscovery.class">
  110. <title>Class autodiscovering</title>
  111. <para>
  112. If class is used to provide SOAP server functionality, then the same class should be provided to
  113. <classname>Zend_Soap_AutoDiscover</classname> for WSDL generation:
  114. <programlisting language="php"><![CDATA[
  115. $autodiscover = new Zend_Soap_AutoDiscover();
  116. $autodiscover->setClass('My_SoapServer_Class');
  117. $autodiscover->handle();
  118. ]]></programlisting>
  119. </para>
  120. <para>
  121. The following rules are used while WSDL generation:
  122. <itemizedlist>
  123. <listitem>
  124. <para>Generated WSDL describes an RPC style Web Service.</para>
  125. </listitem>
  126. <listitem>
  127. <para>Class name is used as a name of the Web Service being described.</para>
  128. </listitem>
  129. <listitem>
  130. <para>
  131. <code>'http://' .$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']</code> is used
  132. as an URI where the WSDL is available by default but can be overwritten via <code>setUri()</code> method.
  133. </para>
  134. <para>
  135. It's also used as a target namespace for all service related names (including
  136. described complex types).
  137. </para>
  138. </listitem>
  139. <listitem>
  140. <para>
  141. Class methods are joined into one <ulink url="http://www.w3.org/TR/wsdl#_porttypes">Port Type</ulink>.
  142. </para>
  143. <para>
  144. <code>$className . 'Port'</code> is used as Port Type name.
  145. </para>
  146. </listitem>
  147. <listitem>
  148. <para>Each class method is registered as a corresponding port operation.</para>
  149. </listitem>
  150. <listitem>
  151. <para>Each method prototype generates corresponding Request/Response messages.</para>
  152. <para>Method may have several prototypes if some method parameters are optional.</para>
  153. </listitem>
  154. </itemizedlist>
  155. </para>
  156. <note>
  157. <title>Important!</title>
  158. <para>
  159. WSDL autodiscovery utilizes the PHP docblocks provided by the developer to determine the parameter and return types.
  160. In fact, for scalar types, this is the only way to determine the parameter types, and for return types, this is the
  161. only way to determine them.
  162. </para>
  163. <para>
  164. That means, providing correct and fully detailed docblocks is not only best practice, but is required for discovered class.
  165. </para>
  166. </note>
  167. </sect2>
  168. <sect2 id="zend.soap.autodiscovery.functions">
  169. <title>Functions autodiscovering</title>
  170. <para>
  171. If set of functions are used to provide SOAP server functionality, then the same set should be provided to
  172. <classname>Zend_Soap_AutoDiscovery</classname> for WSDL generation:
  173. <programlisting language="php"><![CDATA[
  174. $autodiscover = new Zend_Soap_AutoDiscover();
  175. $autodiscover->addFunction('function1');
  176. $autodiscover->addFunction('function2');
  177. $autodiscover->addFunction('function3');
  178. ...
  179. $autodiscover->handle();
  180. ]]></programlisting>
  181. </para>
  182. <para>
  183. The following rules are used while WSDL generation:
  184. <itemizedlist>
  185. <listitem>
  186. <para>Generated WSDL describes an RPC style Web Service.</para>
  187. </listitem>
  188. <listitem>
  189. <para>Current script name is used as a name of the Web Service being described.</para>
  190. </listitem>
  191. <listitem>
  192. <para>
  193. <code>'http://' .$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']</code> is used
  194. as an URI where the WSDL is available.
  195. </para>
  196. <para>
  197. It's also used as a target namespace for all service related names (including
  198. described complex types).
  199. </para>
  200. </listitem>
  201. <listitem>
  202. <para>
  203. Functions are joined into one <ulink url="http://www.w3.org/TR/wsdl#_porttypes">Port Type</ulink>.
  204. </para>
  205. <para>
  206. <code>$functionName . 'Port'</code> is used as Port Type name.
  207. </para>
  208. </listitem>
  209. <listitem>
  210. <para>Each function is registered as a corresponding port operation.</para>
  211. </listitem>
  212. <listitem>
  213. <para>Each function prototype generates corresponding Request/Response messages.</para>
  214. <para>Function may have several prototypes if some method parameters are optional.</para>
  215. </listitem>
  216. </itemizedlist>
  217. </para>
  218. <note>
  219. <title>Important!</title>
  220. <para>
  221. WSDL autodiscovery utilizes the PHP docblocks provided by the developer to determine the parameter and return types.
  222. In fact, for scalar types, this is the only way to determine the parameter types, and for return types, this is the
  223. only way to determine them.
  224. </para>
  225. <para>
  226. That means, providing correct and fully detailed docblocks is not only best practice, but is required for discovered class.
  227. </para>
  228. </note>
  229. </sect2>
  230. <sect2 id="zend.soap.autodiscovery.datatypes">
  231. <title>Autodiscovering Datatypes</title>
  232. <para>
  233. Input/output datatypes are converted into network service types using the following mapping:
  234. <itemizedlist>
  235. <listitem>
  236. <para>PHP strings &lt;-&gt; <code>xsd:string</code>.</para>
  237. </listitem>
  238. <listitem>
  239. <para>PHP integers &lt;-&gt; <code>xsd:int</code>.</para>
  240. </listitem>
  241. <listitem>
  242. <para>PHP floats and doubles &lt;-&gt; <code>xsd:float</code>.</para>
  243. </listitem>
  244. <listitem>
  245. <para>PHP booleans &lt;-&gt; <code>xsd:boolean</code>.</para>
  246. </listitem>
  247. <listitem>
  248. <para>PHP arrays &lt;-&gt; <code>soap-enc:Array</code>.</para>
  249. </listitem>
  250. <listitem>
  251. <para>PHP object &lt;-&gt; <code>xsd:struct</code>.</para>
  252. </listitem>
  253. <listitem>
  254. <para>
  255. PHP class &lt;-&gt; based on complex type strategy (See: <xref linkend="zend.soap.wsdl.types.add_complex" />)
  256. <footnote>
  257. <para>
  258. <classname>Zend_Soap_AutoDiscover</classname> will be created with the <classname>Zend_Soap_Wsdl_Strategy_DefaultComplexType</classname>
  259. class as detection algorithm for complex types. The first parameter of the AutoDiscover constructor takes
  260. any complex type strategy implementing <classname>Zend_Soap_Wsdl_Strategy_Interface</classname> or a string with the name
  261. of the class. For backwards compatibility with <code>$extractComplexType</code> boolean variables are parsed
  262. exactly like in <classname>Zend_Soap_Wsdl</classname>. See the <link linkend="zend.soap.wsdl.types.add_complex"><classname>Zend_Soap_Wsdl</classname> manual
  263. on adding complex</link> types for more information.
  264. </para>
  265. </footnote>.
  266. </para>
  267. </listitem>
  268. <listitem>
  269. <para>type[] or object[] (ie. int[]) &lt;-&gt; based on complex type strategy</para>
  270. </listitem>
  271. <listitem>
  272. <para>PHP void &lt;-&gt; empty type.</para>
  273. </listitem>
  274. <listitem>
  275. <para>If type is not matched to any of these types by some reason, then <code>xsd:anyType</code> is used.</para>
  276. </listitem>
  277. </itemizedlist>
  278. Where <code>xsd:</code> is "http://www.w3.org/2001/XMLSchema" namespace,
  279. <code>soap-enc:</code> is a "http://schemas.xmlsoap.org/soap/encoding/" namespace,
  280. <code>tns:</code> is a "target namespace" for a service.
  281. </para>
  282. </sect2>
  283. <sect2 id="zend.soap.autodiscovery.wsdlstyles">
  284. <title>WSDL Binding Styles</title>
  285. <para>
  286. WSDL offers different transport mechanisms and styles. This affects the
  287. <code>soap:binding</code> and <code>soap:body</code> tags within the Binding
  288. section of WSDL. Different clients have different requirements as to what options
  289. really work. Therefore you can set the styles before you call any <code>setClass</code>
  290. or <code>addFunction</code> method on the AutoDiscover class.
  291. </para>
  292. <para>
  293. <programlisting language="php"><![CDATA[
  294. $autodiscover = new Zend_Soap_AutoDiscover();
  295. // Default is 'use' => 'encoded' and
  296. // 'encodingStyle' => 'http://schemas.xmlsoap.org/soap/encoding/'
  297. $autodiscover->setOperationBodyStyle(
  298. array('use' => 'literal',
  299. 'namespace' => 'http://framework.zend.com')
  300. );
  301. // Default is 'style' => 'rpc' and
  302. // 'transport' => 'http://schemas.xmlsoap.org/soap/http'
  303. $autodiscover->setBindingStyle(
  304. array('style' => 'document',
  305. 'transport' => 'http://framework.zend.com')
  306. );
  307. ...
  308. $autodiscover->addFunction('myfunc1');
  309. $autodiscover->handle();
  310. ]]></programlisting>
  311. </para>
  312. </sect2>
  313. </sect1>