Zend_Soap_AutoDiscovery.xml 17 KB

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