PHP Soapserver を使用して PHP で SOAP サーバーを作成しました。最初の引数と対応する wsdl ドキュメントに複数の項目を配列として追加する関数「addItems」を作成しました。wsdl で、次のような配列オブジェクト 'items' を作成しました。
<s:complexType name="items">
<s:sequence>
<s:element nillable="true" name="item" type="tns:item" minOccurs="0" maxOccurs="unbounded"/>
</s:sequence>
</s:complexType>
php 関数は、次のように php soapserver に渡される単純なクラス メソッドです。
<?php
class Test
{
public function addItems($items) {
print_r($items);
}
}
$service = new Test();
$server = new SoapServer('wsdl.xml');
$server->setObject();
print($server->handle());
問題は、php soapserver が配列を処理する方法です。項目を 1 つだけ追加すると、項目リストが配列として返されません。2 つのアイテムを追加すると、アイテムの配列は、予想されるアイテム引数ではなくアイテム要素に格納されます。
soapenc:array を使用してみましたが、http://www.ws-i.org/Profiles/BasicProfile-1.0-2004-04-16.html#refinement16556272 に記載されているように WS-I 準拠と互換性がないようで、失敗しますこのタイプのエンコーディングを使用するときに、ビジュアルスタジオにサービスを追加したいとき。
それで、私は何を間違っていますか?どこかでwsdlを変更する必要があると思いますが、実際の例が見つかりませんでした。または、soapserver にいくつかのオプションを設定する必要がありますか? 道に迷いました。
以下に、リクエスト、実際の結果、および期待される結果を示します。
1つのアイテムでリクエスト:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:addItems" xmlns:wsdl="http://base.shopzonline.com/soap/api/test/?wsdl">
<soapenv:Header/>
<soapenv:Body>
<urn:addItems>
<items>
<!--Zero or more repetitions:-->
<wsdl:item>
<wsdl:id>1</wsdl:id>
<wsdl:name>a</wsdl:name>
</wsdl:item>
</items>
</urn:addItems>
</soapenv:Body>
</soapenv:Envelope>
$items 引数の実際の結果
Array
(
[item] => Array
(
[id] => 1
[name] => a
)
)
$items 引数の期待される結果
Array
[0] => (
Array
(
[id] => 1
[name] => a
)
)
複数のリクエスト:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:addItems" xmlns:wsdl="http://base.shopzonline.com/soap/api/test/?wsdl">
<soapenv:Header/>
<soapenv:Body>
<urn:addItems>
<items>
<!--Zero or more repetitions:-->
<wsdl:item>
<wsdl:id>1</wsdl:id>
<wsdl:name>a</wsdl:name>
</wsdl:item>
<wsdl:item>
<wsdl:id>2</wsdl:id>
<wsdl:name>b</wsdl:name>
</wsdl:item>
</items>
</urn:addItems>
</soapenv:Body>
</soapenv:Envelope>
$items 引数の実際の結果
Array
(
[item] => Array
(
[0] => Array
(
[id] => 1
[name] => a
)
[1] => Array
(
[id] => 2
[name] => b
)
)
)
期待される結果
Array
(
[0] => Array
(
[id] => 1
[name] => a
)
[1] => Array
(
[id] => 2
[name] => b
)
)
完全な WSDL の下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions name="LocalhostTestWebservice"
targetNamespace="http://localhost/soap/api/test/?wsdl"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://localhost/soap/api/test/?wsdl"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:s="http://www.w3.org/2001/XMLSchema"
>
<wsdl:types>
<s:schema elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://localhost/soap/api/test/?wsdl">
<s:complexType name="addItemsResponseElement">
<s:sequence>
<s:element name="result" minOccurs="0" type="s:int"/>
</s:sequence>
</s:complexType>
<s:element name="addItemsResponseElement" nillable="true" type="tns:addItemsResponseElement" />
<s:complexType name="item">
<s:sequence>
<s:element name="id" type="s:string" />
<s:element name="name" type="s:string" />
</s:sequence>
</s:complexType>
<s:element name="item" type="tns:item" />
<s:complexType name="items">
<s:sequence>
<s:element nillable="true" name="item" type="tns:item" minOccurs="0" maxOccurs="unbounded"/>
</s:sequence>
</s:complexType>
<s:element name="items" type="tns:items" />
</s:schema>
</wsdl:types>
<wsdl:message name="addItemsRequest">
<wsdl:part name="items" type="tns:items" />
</wsdl:message>
<wsdl:message name="addItemsResponse">
<wsdl:part name="parameters" type="tns:addItemsResponseElement"/>
</wsdl:message>
<wsdl:portType name="TestPortType">
<wsdl:operation name="addItems">
<wsdl:input message="tns:addItemsRequest" />
<wsdl:output message="tns:addItemsResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="LocalhostTestWebserviceBinding" type="tns:TestPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="addItems">
<soap:operation soapAction="addItems" />
<wsdl:input>
<soap:body use="literal" namespace="urn:addItems" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" namespace="urn:addItems" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="LocalhostTestWebservice">
<wsdl:port name="LocalhostTestWebservicePort" binding="tns:LocalhostTestWebserviceBinding">
<soap:address location="http://localhost/api/soap/test/" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>