2
0

Zend_Soap_AutoDiscovery.xml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. </para>
  84. <programlisting language="php"><![CDATA[
  85. class My_SoapServer_Class {
  86. ...
  87. }
  88. $autodiscover = new Zend_Soap_AutoDiscover();
  89. $autodiscover->setClass('My_SoapServer_Class');
  90. $autodiscover->handle();
  91. ]]></programlisting>
  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. <programlisting language="php"><![CDATA[
  114. if(isset($_GET['wsdl'])) {
  115. $autodiscover = new Zend_Soap_AutoDiscover();
  116. $autodiscover->setClass('HelloWorldService');
  117. $autodiscover->handle();
  118. } else {
  119. // pointing to the current file here
  120. $soap = new Zend_Soap_Server("http://example.com/soap.php?wsdl");
  121. $soap->setClass('HelloWorldService');
  122. $soap->handle();
  123. }
  124. ]]></programlisting>
  125. </note>
  126. </sect2>
  127. <sect2 id="zend.soap.autodiscovery.class">
  128. <title>Class autodiscovering</title>
  129. <para>
  130. If class is used to provide SOAP server functionality, then the same class should be
  131. provided to <classname>Zend_Soap_AutoDiscover</classname> for WSDL generation:
  132. </para>
  133. <programlisting language="php"><![CDATA[
  134. $autodiscover = new Zend_Soap_AutoDiscover();
  135. $autodiscover->setClass('My_SoapServer_Class');
  136. $autodiscover->handle();
  137. ]]></programlisting>
  138. <para>
  139. The following rules are used while WSDL generation:
  140. <itemizedlist>
  141. <listitem>
  142. <para>Generated WSDL describes an RPC style Web Service.</para>
  143. </listitem>
  144. <listitem>
  145. <para>Class name is used as a name of the Web Service being described.</para>
  146. </listitem>
  147. <listitem>
  148. <para>
  149. <code>'http://' .$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']</code> is
  150. used as an <acronym>URI</acronym> where the WSDL is available by default but
  151. can be overwritten via <methodname>setUri()</methodname> method.
  152. </para>
  153. <para>
  154. It's also used as a target namespace for all service related names
  155. (including described complex types).
  156. </para>
  157. </listitem>
  158. <listitem>
  159. <para>
  160. Class methods are joined into one <ulink
  161. url="http://www.w3.org/TR/wsdl#_porttypes">Port Type</ulink>.
  162. </para>
  163. <para>
  164. <code>$className . 'Port'</code> is used as Port Type name.
  165. </para>
  166. </listitem>
  167. <listitem>
  168. <para>Each class method is registered as a corresponding port operation.</para>
  169. </listitem>
  170. <listitem>
  171. <para>
  172. Each method prototype generates corresponding Request/Response messages.
  173. </para>
  174. <para>
  175. Method may have several prototypes if some method parameters are optional.
  176. </para>
  177. </listitem>
  178. </itemizedlist>
  179. </para>
  180. <note>
  181. <title>Important!</title>
  182. <para>
  183. WSDL autodiscovery utilizes the <acronym>PHP</acronym> docblocks provided by the
  184. developer to determine the parameter and return types. In fact, for scalar types,
  185. this is the only way to determine the parameter types, and for return types, this is
  186. the only way to determine them.
  187. </para>
  188. <para>
  189. That means, providing correct and fully detailed docblocks is not only best
  190. practice, but is required for discovered class.
  191. </para>
  192. </note>
  193. </sect2>
  194. <sect2 id="zend.soap.autodiscovery.functions">
  195. <title>Functions autodiscovering</title>
  196. <para>
  197. If set of functions are used to provide SOAP server functionality, then the same set
  198. should be provided to <classname>Zend_Soap_AutoDiscovery</classname> for WSDL
  199. generation:
  200. </para>
  201. <programlisting language="php"><![CDATA[
  202. $autodiscover = new Zend_Soap_AutoDiscover();
  203. $autodiscover->addFunction('function1');
  204. $autodiscover->addFunction('function2');
  205. $autodiscover->addFunction('function3');
  206. ...
  207. $autodiscover->handle();
  208. ]]></programlisting>
  209. <para>
  210. The following rules are used while WSDL generation:
  211. <itemizedlist>
  212. <listitem>
  213. <para>Generated WSDL describes an RPC style Web Service.</para>
  214. </listitem>
  215. <listitem>
  216. <para>
  217. Current script name is used as a name of the Web Service being described.
  218. </para>
  219. </listitem>
  220. <listitem>
  221. <para>
  222. <code>'http://' .$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']</code> is
  223. used as an <acronym>URI</acronym> where the WSDL is available.
  224. </para>
  225. <para>
  226. It's also used as a target namespace for all service related names
  227. (including described complex types).
  228. </para>
  229. </listitem>
  230. <listitem>
  231. <para>
  232. Functions are joined into one <ulink
  233. url="http://www.w3.org/TR/wsdl#_porttypes">Port Type</ulink>.
  234. </para>
  235. <para>
  236. <code>$functionName . 'Port'</code> is used as Port Type name.
  237. </para>
  238. </listitem>
  239. <listitem>
  240. <para>Each function is registered as a corresponding port operation.</para>
  241. </listitem>
  242. <listitem>
  243. <para>
  244. Each function prototype generates corresponding Request/Response messages.
  245. </para>
  246. <para>
  247. Function may have several prototypes if some method parameters are optional.
  248. </para>
  249. </listitem>
  250. </itemizedlist>
  251. </para>
  252. <note>
  253. <title>Important!</title>
  254. <para>
  255. WSDL autodiscovery utilizes the <acronym>PHP</acronym> docblocks provided by the
  256. developer to determine the parameter and return types. In fact, for scalar types,
  257. this is the only way to determine the parameter types, and for return types, this is
  258. the only way to determine them.
  259. </para>
  260. <para>
  261. That means, providing correct and fully detailed docblocks is not only best
  262. practice, but is required for discovered class.
  263. </para>
  264. </note>
  265. </sect2>
  266. <sect2 id="zend.soap.autodiscovery.datatypes">
  267. <title>Autodiscovering Datatypes</title>
  268. <para>
  269. Input/output datatypes are converted into network service types using the following
  270. mapping:
  271. <itemizedlist>
  272. <listitem>
  273. <para>PHP strings &lt;-&gt; <code>xsd:string</code>.</para>
  274. </listitem>
  275. <listitem>
  276. <para>PHP integers &lt;-&gt; <code>xsd:int</code>.</para>
  277. </listitem>
  278. <listitem>
  279. <para>PHP floats and doubles &lt;-&gt; <code>xsd:float</code>.</para>
  280. </listitem>
  281. <listitem>
  282. <para>PHP booleans &lt;-&gt; <code>xsd:boolean</code>.</para>
  283. </listitem>
  284. <listitem>
  285. <para>PHP arrays &lt;-&gt; <code>soap-enc:Array</code>.</para>
  286. </listitem>
  287. <listitem>
  288. <para>PHP object &lt;-&gt; <code>xsd:struct</code>.</para>
  289. </listitem>
  290. <listitem>
  291. <para>
  292. <acronym>PHP</acronym> class &lt;-&gt; based on complex type strategy (See:
  293. <link linkend="zend.soap.wsdl.types.add_complex">this section</link>)
  294. <footnote>
  295. <para>
  296. <classname>Zend_Soap_AutoDiscover</classname> will be created with
  297. the
  298. <classname>Zend_Soap_Wsdl_Strategy_DefaultComplexType</classname>
  299. class as detection algorithm for complex types. The first parameter
  300. of the AutoDiscover constructor takes any complex type strategy
  301. implementing
  302. <classname>Zend_Soap_Wsdl_Strategy_Interface</classname> or a string
  303. with the name of the class. For backwards compatibility with
  304. <varname>$extractComplexType</varname> boolean variables are parsed
  305. exactly like in <classname>Zend_Soap_Wsdl</classname>. See the
  306. <link
  307. linkend="zend.soap.wsdl.types.add_complex"><classname>Zend_Soap_Wsdl</classname>
  308. manual on adding complex</link> types for more information.
  309. </para>
  310. </footnote>.
  311. </para>
  312. </listitem>
  313. <listitem>
  314. <para>
  315. type[] or object[] (ie. int[]) &lt;-&gt; based on complex type strategy
  316. </para>
  317. </listitem>
  318. <listitem>
  319. <para>PHP void &lt;-&gt; empty type.</para>
  320. </listitem>
  321. <listitem>
  322. <para>
  323. If type is not matched to any of these types by some reason, then
  324. <code>xsd:anyType</code> is used.
  325. </para>
  326. </listitem>
  327. </itemizedlist>
  328. Where <code>xsd:</code> is "http://www.w3.org/2001/XMLSchema" namespace,
  329. <code>soap-enc:</code> is a "http://schemas.xmlsoap.org/soap/encoding/" namespace,
  330. <code>tns:</code> is a "target namespace" for a service.
  331. </para>
  332. </sect2>
  333. <sect2 id="zend.soap.autodiscovery.wsdlstyles">
  334. <title>WSDL Binding Styles</title>
  335. <para>
  336. WSDL offers different transport mechanisms and styles. This affects the
  337. <code>soap:binding</code> and <code>soap:body</code> tags within the Binding
  338. section of WSDL. Different clients have different requirements as to what options
  339. really work. Therefore you can set the styles before you call any <code>setClass</code>
  340. or <code>addFunction</code> method on the AutoDiscover class.
  341. </para>
  342. <programlisting language="php"><![CDATA[
  343. $autodiscover = new Zend_Soap_AutoDiscover();
  344. // Default is 'use' => 'encoded' and
  345. // 'encodingStyle' => 'http://schemas.xmlsoap.org/soap/encoding/'
  346. $autodiscover->setOperationBodyStyle(
  347. array('use' => 'literal',
  348. 'namespace' => 'http://framework.zend.com')
  349. );
  350. // Default is 'style' => 'rpc' and
  351. // 'transport' => 'http://schemas.xmlsoap.org/soap/http'
  352. $autodiscover->setBindingStyle(
  353. array('style' => 'document',
  354. 'transport' => 'http://framework.zend.com')
  355. );
  356. ...
  357. $autodiscover->addFunction('myfunc1');
  358. $autodiscover->handle();
  359. ]]></programlisting>
  360. </sect2>
  361. </sect1>