【问题标题】:Accessing a SOAP Webservice using PHP containing a complexType in the WSDL使用 WSDL 中包含 complexType 的 PHP 访问 SOAP Web 服务
【发布时间】:2011-11-15 05:14:01
【问题描述】:

我想调用一个只有 WSDL 文件的 Web 服务。我将列出 WSDL 文件中的重要位,并将上下文相关的名称替换为通用名称:

我要调用的函数:

<wsdl:operation name="myFunction">
   <wsdl:input message="ns:myFunctionRequest" wsaw:Action="urn:myFunction"/>
   <wsdl:output message="ns:myFunctionResponse" wsaw:Action="urn:myFunctionResponse"/>
</wsdl:operation>

函数说明:

<xs:element name="myFunction">
   <xs:complexType>
      <xs:sequence>
         <xs:element minOccurs="0" name="param0" nillable="true" type="xs:string"/>
         <xs:element minOccurs="0" name="param1" nillable="true" type="somens:MyType"/>
         <xs:element minOccurs="0" name="param2" nillable="true" type="xs:string"/>
      </xs:sequence>
   </xs:complexType>
</xs:element>

'MyType'的描述:

<xs:complexType name="MyType">
    <xs:sequence>
        <xs:element minOccurs="0" name="date1" nillable="true" type="xs:dateTime"/>
        <xs:element minOccurs="0" name="date2" nillable="true" type="xs:dateTime"/>
        <xs:element minOccurs="0" name="string1" nillable="true" type="xs:string"/>
        <xs:element minOccurs="0" name="int1" nillable="true" type="xs:int"/>
        <xs:element minOccurs="0" name="int2" nillable="true" type="xs:int"/>
    </xs:sequence>
</xs:complexType>

我的 PHP 代码看起来有点像这样:

$client = new SOAPClient('my soap URL');

$result = $client->setState(array('param0'=>'bla', 'param1'=><undecided>, 'param2'=>'bla');

我的问题是在未决定的部分放什么。那应该是一个将复杂类型中的名称映射到值的数组吗?还是应该是我用相应类型的字段定义自己的类?目前我没有一个很好的试验场来尝试这个,我希望能够在它可用之前继续前进。

我看到很多与 SOAP 相关的问题没有得到解答,所以我希望我能走运 :)。如果我碰巧发现自己,我当然会分享我的结果。

提前致谢!

【问题讨论】:

    标签: php web-services soap wsdl complextype


    【解决方案1】:

    根据我的经验,您需要一个对象而不是数组,但 stdClass 就足够了,所以如果它让生活更轻松,您可以将数组转换为对象:

    $client->myFunction(
        'bla',
        (object)array(
            'date1'   => '2010-01-01 00:00:00',
            'date2'   => '2010-01-02 00:00:00',
            'string1' => 'foobar',
            'int1'    => 1,
            'int2'    => 2
        ),
        'bla');
    

    【讨论】:

    • 谢谢!这非常简明扼要。正是我需要的。我将在这里发表另一条评论,以确认它在我必须能够测试它时起作用。
    • Goed om the horen & dank je voor de beleefdheid om even te melden of het inderdaad werkte of niet, dat heeft helaas niet iedereen ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多