【原】使用wsdl的注意事项 – 胡健 – 博客园

如果你对wsdl文件尚不熟悉,请参考我的这篇文章:【原】原生态手写wsdl文件。

1、之前将wsdl文件中的message定义如下:

<span style="color: #008080;">1</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">wsdl:message </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="MinusIn"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">2</span>        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">wsdl:part </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="parameters1"</span><span style="color: #ff0000;"> element</span><span style="color: #0000ff;">="s::int"</span><span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">3</span>        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">wsdl:part </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="parameters2"</span><span style="color: #ff0000;"> element</span><span style="color: #0000ff;">="s::int"</span><span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">4</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">wsdl:message</span><span style="color: #0000ff;">&gt;</span>

    再新加一个Minus2接口,想通过数组返回多个值时,发现调用的老是Minus接口; 删除Minus接口的定义后,发现返回的还是不正确。 后来通过仔细比较发现,这里应当写成: 

<span style="color: #008080;">1</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">wsdl:message </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="MinusIn"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">2</span>       <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">wsdl:part </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="parameters1"</span><span style="color: #ff0000;"> element</span><span style="color: #0000ff;">="s:int"</span><span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">3</span>       <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">wsdl:part </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="parameters2"</span><span style="color: #ff0000;"> element</span><span style="color: #0000ff;">="s:int"</span><span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">4</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">wsdl:message</span><span style="color: #0000ff;">&gt;</span>

    多写了一个冒号,导致弄出了很多比较奇怪的问题。 另外,如果wsdl:message下有多个参数wsdl:part,则它们的name属性一定不能相同,否则Server端两个参数的值会一样,都取Client端第一个参数的值。

 

2、当想返回多个值(注意,不是将多个值放到一个自定义类中)时,在php中Server端返回一个关联数组即可,比如:

<span style="color: #008080;">1</span> <span style="color: #0000ff;">function</span> Minus2(<span style="color: #800080;">$i</span>, <span style="color: #800080;">$j</span><span style="color: #000000;">)</p><p></span><span style="color: #008080;">2</span> <span style="color: #000000;">{</p><p></span><span style="color: #008080;">3</span>       <span style="color: #800080;">$add</span> = <span style="color: #800080;">$i</span> + <span style="color: #800080;">$j</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;">4</span>       <span style="color: #800080;">$sub</span> = <span style="color: #800080;">$i</span> - <span style="color: #800080;">$j</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;">5</span>       <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">array</span>("add"=&gt;<span style="color: #800080;">$add</span>, "sub"=&gt;<span style="color: #800080;">$sub</span><span style="color: #000000;">);</p><p></span><span style="color: #008080;">6</span> }

    但在wsdl中,有两种写法:

    一种,是直接在message中添加多个返回值: 

<span style="color: #008080;">1</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">wsdl:message </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="MinusOut"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">2</span>        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">wsdl:part </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="add"</span><span style="color: #ff0000;"> element</span><span style="color: #0000ff;">="s:int"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">3</span>        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">wsdl:part </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="sub"</span><span style="color: #ff0000;"> element</span><span style="color: #0000ff;">="s:int"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">4</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">wsdl:message</span><span style="color: #0000ff;">&gt;</span>

    注意:上面wsdl:part的name属性的值必须与Server端返回的数组中的key一致,Client端也需要用这个name作为key来取回数组中的值。 此时在Client端获取的值,也是一个关联数组,var_dump的结果如下: 

<span style="color: #008080;">1</span> <span style="color: #800080;">$ret</span> = <span style="color: #800080;">$client</span>-&gt;Minus2(100, 99<span style="color: #000000;">);</p><p></span><span style="color: #008080;">2</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$ret</span><span style="color: #000000;">);</p><p></span><span style="color: #008080;">3</span> <span style="color: #0000ff;">array</span>(2) { 'add' =&gt; <span style="color: #0000ff;">string</span>(3) "199" 'sub' =&gt; <span style="color: #0000ff;">string</span>(1) "1" }

    另一种,是在wsdl:types中定义一个包含多个类型的复杂类型,然后在message中指定这个复杂类型。Server端代码与上面相同,wsdl文件如下:    

复制代码

<span style="color: #008080;">1</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="MinusType"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">2</span>      <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">3</span>           <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">4</span>                 <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="0"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="add"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:int"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">5</span>                 <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="0"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="sub"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:int"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">6</span>             <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">7</span>       <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">8</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:element</span><span style="color: #0000ff;">&gt;</span>

复制代码

    注意: 上面s:element的name属性的值也必须与Server端返回的数组中的key一致,Client端需要使用这个name作为成员变量名来访问对象中的成员变量值。此时,Client端返回的是一个类,结果如下:   

<span style="color: #008080;">1</span> <span style="color: #800080;">$ret</span> = <span style="color: #800080;">$client</span>-&gt;Minus2(100,99<span style="color: #000000;">);</p><p></span><span style="color: #008080;">2</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$ret</span><span style="color: #000000;">);</p><p></span><span style="color: #008080;">3</span> stdClass<span style="color: #008000;">#</span><span style="color: #008000;">2 (2) { public $add =&gt; int(199) public $sub =&gt; int(1) }</span>

 

3、当想传入或返回的是一个自定义的结构体时,则需要在wsdl中定义复杂类型,同时在Server端定义类,类的成员变量名就是复杂类型包含的子类型的name字段的值,Client端返回的也是一个匿名类。 示例代码如下:

复制代码

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> TMinus</p><p></span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{</p><p></span><span style="color: #008080;"> 3</span>        <span style="color: #0000ff;">var</span> <span style="color: #800080;">$add</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;"> 4</span>        <span style="color: #0000ff;">var</span> <span style="color: #800080;">$sub</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;"> 5</span> <span style="color: #000000;">}</p><p></span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">function</span> Minus2(<span style="color: #800080;">$i</span>, <span style="color: #800080;">$j</span><span style="color: #000000;">)</p><p></span><span style="color: #008080;"> 7</span> <span style="color: #000000;">{</p><p></span><span style="color: #008080;"> 8</span>       <span style="color: #800080;">$minus</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> TMinus;</p><p></span><span style="color: #008080;"> 9</span>       <span style="color: #800080;">$minus</span>-&gt;add = <span style="color: #800080;">$i</span> + <span style="color: #800080;">$j</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;">10</span>       <span style="color: #800080;">$minus</span>-&gt;sub = <span style="color: #800080;">$i</span> - <span style="color: #800080;">$j</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;">11</span>       <span style="color: #0000ff;">return</span> <span style="color: #800080;">$minus</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;">12</span> }

复制代码

    此时,Client端var_dump的结果 为:

<span style="color: #008080;">1</span> <span style="color: #0000ff;">class</span> stdClass<span style="color: #008000;">#</span><span style="color: #008000;">2 (2) { public $add =&gt; int(199) public $sub =&gt; int(1) }</span>

 

4、当想传入或返回一个数组时,需要定义soap数组,如下:

复制代码

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="CustomType"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 2</span>     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 3</span>          <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 4</span>               <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="0"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="sessionId"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:string"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;"> 5</span>               <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="0"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="fileSize"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:string"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;"> 6</span>           <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 7</span>       <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 8</span>  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:element</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 9</span>  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="CustomTypeArray"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">10</span>       <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">11</span>             <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexContent</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">12</span>                   <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:restriction </span><span style="color: #ff0000;">base</span><span style="color: #0000ff;">="soapenc:Array"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">13</span>                         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:attribute </span><span style="color: #ff0000;">ref</span><span style="color: #0000ff;">="soapenc:arrayType"</span><span style="color: #ff0000;"> wsdl:arrayType</span><span style="color: #0000ff;">="tns:CustomType[]"</span><span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">14</span>                   <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:restriction</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">15</span>            <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexContent</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">16</span>       <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">17</span>  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:element</span><span style="color: #0000ff;">&gt;</span>

复制代码

    Server端需要定义一个类,并返回这个类的实例的数组,如下:

复制代码

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> CustomType</p><p></span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{</p><p></span><span style="color: #008080;"> 3</span>        <span style="color: #0000ff;">public</span> <span style="color: #800080;">$sessionId</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;"> 4</span>        <span style="color: #0000ff;">public</span> <span style="color: #800080;">$fileSize</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;"> 5</span>        <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$sessionID</span>, <span style="color: #800080;">$fileSize</span><span style="color: #000000;">)</p><p></span><span style="color: #008080;"> 6</span> <span style="color: #000000;">       {</p><p></span><span style="color: #008080;"> 7</span>              <span style="color: #800080;">$this</span>-&gt;sessionId = <span style="color: #800080;">$sessionID</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;"> 8</span>              <span style="color: #800080;">$this</span>-&gt;<span style="color: #008080;">fileSize</span> = <span style="color: #800080;">$fileSize</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;"> 9</span> <span style="color: #000000;">       }</p><p></span><span style="color: #008080;">10</span> <span style="color: #000000;">}</p><p></span><span style="color: #008080;">11</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> GetData()</p><p></span><span style="color: #008080;">12</span> <span style="color: #000000;">{</p><p></span><span style="color: #008080;">13</span>        <span style="color: #800080;">$data</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();</p><p></span><span style="color: #008080;">14</span>        <span style="color: #800080;">$data</span>[] = <span style="color: #0000ff;">new</span> CustomType("123", "456"<span style="color: #000000;">);</p><p></span><span style="color: #008080;">15</span>        <span style="color: #800080;">$data</span>[] = <span style="color: #0000ff;">new</span> CustomType("789", "912"<span style="color: #000000;">);</p><p></span><span style="color: #008080;">16</span>        <span style="color: #0000ff;">return</span> <span style="color: #800080;">$data</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;">17</span> }

复制代码

    Client端对返回的数组进行遍历即可:

<span style="color: #008080;">1</span> <span style="color: #800080;">$data</span> = <span style="color: #800080;">$client</span>-&gt;<span style="color: #000000;">GetData(); </p><p></span><span style="color: #008080;">2</span> <span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$data</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$item</span><span style="color: #000000;">) </p><p></span><span style="color: #008080;">3</span> <span style="color: #000000;">{ </p><p></span><span style="color: #008080;">4</span>      <span style="color: #0000ff;">echo</span> "&lt;br/&gt;".<span style="color: #800080;">$item</span>-&gt;sessionId." and ".<span style="color: #800080;">$item</span>-&gt;<span style="color: #008080;">fileSize</span>."&lt;br/&gt;"<span style="color: #000000;">; </p><p></span><span style="color: #008080;">5</span> }

 

5、如果将简单数据类型放在复杂数据类型中,则Server端和Client端的代码都必须相应作出修改。 wsdl文件如下:

复制代码

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="MyInt"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 2</span>      <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 3</span>            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 4</span>                 <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="0"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="request"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:int"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;"> 5</span>            <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 6</span>      <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:element</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">wsdl:message </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="MinusIn"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 9</span>        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">wsdl:part </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="parameters1"</span><span style="color: #ff0000;"> element</span><span style="color: #0000ff;">="tns:MyInt"</span><span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">10</span>        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">wsdl:part </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="parameters2"</span><span style="color: #ff0000;"> element</span><span style="color: #0000ff;">="tns:MyInt"</span><span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">11</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">wsdl:message</span><span style="color: #0000ff;">&gt;</span>

复制代码

    此时Server端接收到的参数,不再是直接的int型了(虽然MyInt中只有一个整形数据),而是一个类的实例。这个类有一个成员变量为request(即MyInt里面子类型的name属性的值)。Server端修改如下: 

复制代码

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> TMinus</p><p></span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{</p><p></span><span style="color: #008080;"> 3</span>        <span style="color: #0000ff;">var</span> <span style="color: #800080;">$add</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;"> 4</span>        <span style="color: #0000ff;">var</span> <span style="color: #800080;">$sub</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;"> 5</span> <span style="color: #000000;">}</p><p></span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">function</span> Minus2(<span style="color: #800080;">$i</span>, <span style="color: #800080;">$j</span><span style="color: #000000;">)</p><p></span><span style="color: #008080;"> 7</span> <span style="color: #000000;">{</p><p></span><span style="color: #008080;"> 8</span>        <span style="color: #800080;">$minus</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> TMinus;</p><p></span><span style="color: #008080;"> 9</span>        <span style="color: #800080;">$minus</span>-&gt;add = <span style="color: #800080;">$i</span>-&gt;<span style="color: #000000;">request;</p><p></span><span style="color: #008080;">10</span>        <span style="color: #800080;">$minus</span>-&gt;sub = <span style="color: #800080;">$j</span>-&gt;<span style="color: #000000;">request;</p><p></span><span style="color: #008080;">11</span>        <span style="color: #0000ff;">return</span> <span style="color: #800080;">$minus</span><span style="color: #000000;">;</p><p></span><span style="color: #008080;">12</span> }

复制代码

    Client端也不能直接写$ret = $client->Minus2(100, 99)了,而应当修改为:

<span style="color: #008080;">1</span> <span style="color: #800080;">$ret</span> = <span style="color: #800080;">$client</span>-&gt;Minus2(<span style="color: #0000ff;">array</span>("request"=&gt;100), <span style="color: #0000ff;">array</span>("request"=&gt;99));

    或者 

<span style="color: #008080;">1</span> <span style="color: #800080;">$ret</span> = <span style="color: #800080;">$client</span>-&gt;__call("Minus2", <span style="color: #0000ff;">array</span>(<span style="color: #0000ff;">array</span>("request"=&gt;100), <span style="color: #0000ff;">array</span>("request"=&gt;99)));

    总结下: 

       a、当wsdl中的接口使用的是复杂类型(就算其只有一个子类型成员)时,Client端传入的参数可以使用array或者Class实例,Server端收到的都是Class实例; Server端传出时,可以使用array和Class实例返回,Client端收到的都是Class实例。 

       b、当wsdl中直接使用简单类型时,如果传入有多个参数,此时对应Client端和Server端传入和接收到多个参数; 如果传出多个参数,Server端返回的应是一个关联数组,Client端接收到的也是一个关联数组。 

 

6、minOccurs和maxOccurs控制该参数出现的次数。如果minOccurs为0,表示该参数可以不存在;如果minOccurs为1,则表示该参数必须存在。如果不存在,会提示类似如下的出错信息:

<span style="color: #008080;">1</span> Error: Client, <span style="color: #0000ff;">string</span>: SOAP-ERROR: Encoding: <span style="color: #0000ff;">object</span> has no 'sub' property。 

 

7、对wsdl中绑定方式的理解,可以参考这篇文章:    

         http://blog.163.com/liuyb_94242/blog/static/4216764620132113914610/ 

    项目刚开始时,一直使用的document/literal模式,没出现什么问题。但在项目中定义了大量接口后,发现接口调用混了,明明Client端调用的是某个接口,但到了Server端,实际调用的却是 另外一个接口。后来阅读上面这篇文章后,发现是绑定方式的问题。document/literal时,接口参数直接写在了Message中,比较简洁,但由于document/literal方式传递soap消息时,只有参数信息,而没有接口名称信息,导致Server端解析时,只会找到第一个匹配参数类型的的接口,因此会出现调混接口的情况。

    理解绑定方式后,有三种解决方案: 

        a、将参数类型完全相同的接口的Mesaage中的part标签的name属性改成不同的值,这样soap消息中参数值的标签就会不一样,Server端就能正确匹配接口。但这种方式不推荐,因为容易出错,并且不利于扩展和维护。

        b、改用rpc/literal方式绑定。因为此方式发送的soap消息中,带有接口名称,故Server端可以正确匹配接口。这种方式一般也不推荐,因为使用远程过程调用RPC会导致程序的耦合度较高,并且不够灵活。  

        c、使用document/literal wrapper模式,推荐使用这种方法。此时接口参数被包含在wrapper中,wrapper一般取接口的名字,这样soap消息中相当于包含了接口的名称,Server端也能正确匹配接口。

 

8、使用document/literal wrapper模式包含数组时,如果像下面这样定义数据类型:

复制代码

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="DepartmentInfo"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 2</span>      <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 3</span>             <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 4</span>                     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="index"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:int"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;"> 5</span>                     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="id"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:string"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;"> 6</span>                     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="name"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:string"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;"> 7</span>             <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 8</span>       <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:element</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">10</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="DepartmentInfoArray"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">11</span>        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">12</span>               <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexContent</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">13</span>                     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:restriction </span><span style="color: #ff0000;">base</span><span style="color: #0000ff;">="soapenc:Array"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">14</span>                            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:attribute </span><span style="color: #ff0000;">ref</span><span style="color: #0000ff;">="soapenc:arrayType"</span><span style="color: #ff0000;"> wsdl:arrayType</span><span style="color: #0000ff;">="tns:DepartmentInfo[]"</span><span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">15</span>                     <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:restriction</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">16</span>               <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexContent</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">17</span>          <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">18</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:element</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">19</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="getAllDepartmentInfo"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">20</span>         <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">21</span>               <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">22</span>                     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="sessionID"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:string"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">23</span>               <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">24</span>        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">25</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:element</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">26</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="getAllDepartmentInfoResponse"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">27</span>        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">28</span>               <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">29</span>                  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="errorCode"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:int"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">30</span>                  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="aDepartmentInfo"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="tns:DepartmentInfoArray"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">31</span>               <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">32</span>        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">33</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:element</span><span style="color: #0000ff;">&gt;</span>

复制代码

     Client端调用成功后,返回的结果中aDepartmentInfo是一个对象(对象中包含一个数组),而不是数组。应当将自定义数据类型和数组类型的element标签删除掉,这样返回的结果才是一个数组。修改后的wsdl文件如下:

复制代码

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="DepartmentInfo"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 2</span>       <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 3</span>              <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="index"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:int"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;"> 4</span>              <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="id"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:string"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;"> 5</span>              <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="name"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:string"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;"> 6</span>        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="DepartmentInfoArray"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 9</span>       <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexContent</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">10</span>              <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:restriction </span><span style="color: #ff0000;">base</span><span style="color: #0000ff;">="soapenc:Array"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">11</span>                     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:attribute </span><span style="color: #ff0000;">ref</span><span style="color: #0000ff;">="soapenc:arrayType"</span><span style="color: #ff0000;"> wsdl:arrayType</span><span style="color: #0000ff;">="tns:DepartmentInfo[]"</span><span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">12</span>               <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:restriction</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">13</span>       <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexContent</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">14</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span>

复制代码

 

9、上面定义数组时,使用的是SOAP数组的方式,但这种方式的兼容性并不好,在用wsdl2objc和WCF时,均无法解析出数组。因此,最好采用下面的方式来定义数组,示例代码如下:

复制代码

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="DepartmentInfo"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 2</span>      <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 3</span>           <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="index"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:int"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;"> 4</span>           <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="id"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:string"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;"> 5</span>           <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="name"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:string"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;"> 6</span>       <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="getAllDepartmentInfo"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;"> 9</span>       <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">10</span>             <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">11</span>                  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="sessionID"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:string"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">12</span>             <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">13</span>        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">14</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:element</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">15</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="getAllDepartmentInfoResponse"</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">16</span>       <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">17</span>            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">18</span>               <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="errorCode"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="s:int"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">19</span>               <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">s:element </span><span style="color: #ff0000;">minOccurs</span><span style="color: #0000ff;">="0"</span><span style="color: #ff0000;"> maxOccurs</span><span style="color: #0000ff;">="unbounded"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="aDepartmentInfo"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="tns:DepartmentInfo"</span> <span style="color: #0000ff;">/&gt;</span></p><p><span style="color: #008080;">20</span>            <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:sequence</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">21</span>       <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:complexType</span><span style="color: #0000ff;">&gt;</span></p><p><span style="color: #008080;">22</span> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">s:element</span><span style="color: #0000ff;">&gt;</span>

复制代码

 

来源URL:http://www.cnblogs.com/hujian/p/3494064.html