我正在尝试编写我的第一个 SOAP 服务器,对 SOAP 客户端做了一些工作。当我尝试发送单个字符串时,效果很好。但是当尝试向服务器发送多个参数时,我会陷入困境。有没有更好的方法来解决这个问题?
服务器:
<?php
if(!extension_loaded("soap")){
dl("php_soap.dll");
}
ini_set("soap.wsdl_cache_enabled","0");
function getCatalogEntry($array){
$conn = mysqli_connect($host,$user,$password,$db) or die(mysqli_error($conn));
$sql = "SELECT '" . $array[0]. "' FROM soap WHERE id = '".$array[1]."'";
$result = $conn->query($sql) or die(mysqli_error($conn));
$row = mysqli_fetch_array($result);
return var_dump($array).$array[0];//$sql.$row[$field];
}
$server = new SoapServer("test.wsdl");
$server->AddFunction("getCatalogEntry");
$server->handle();
?>
客户:
<?php
try{
$sClient = new SoapClient('server.php?wsdl');
$params = array(
"field" => "field2",
"id" => "id");
$response = $sClient->getCatalogEntry($params);
var_dump($response);
} catch(SoapFault $e){
var_dump($e);
}
?>
WSDL:
<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions name='Catalog'
targetNamespace='http://example.org/catalog'
xmlns:tns=' http://example.org/catalog '
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>
<xsd:complexType name="KeyValueData">
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1" name="id" type="string"/>
<xsd:element minOccurs="1" maxOccurs="1" name="field" type="string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ArrayOfKeyValueData">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded"
name="keyval" type="tns:KeyValueData"/>
</xsd:sequence>
</xsd:complexType>
<message name='getCatalogRequest'>
<part name='catalogId' type='ArrayOfKeyValueData'/>
</message>
<message name='getCatalogResponse'>
<part name='Result' type='xsd:string'/>
</message>
<portType name='CatalogPortType'>
<operation name='getCatalogEntry'>
<input message='tns:getCatalogRequest'/>
<output message='tns:getCatalogResponse'/>
</operation>
</portType>
<binding name='CatalogBinding' type='tns:CatalogPortType'>
<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http'
/>
<operation name='getCatalogEntry'>
<soap:operation soapAction='urn:localhost-catalog#
getCatalogEntry'/>
<input>
<soap:body use='encoded' namespace=
'urn:localhost-catalog'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</input>
<output>
<soap:body use='encoded' namespace=
'urn:localhost-catalog'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' />
</output>
</operation>
</binding>
<service name='CatalogService'>
<port name='CatalogPort' binding=
'CatalogBinding'>
<soap:address location='server.php'/>
</port>
</service>
</definitions>