我正在尝试在Zend框架2中创建一个SOAP客户端,我创建了下面的,它正确地返回数据
try {
$client = new Zend\Soap\Client("http://www.webservicex.net/country.asmx?WSDL");
$result = $client->GetCountries();
print_r($result);
} catch (SoapFault $s) {
die('ERROR: [' . $s->faultcode . '] ' . $s->faultstring);
} catch (Exception $e) {
die('ERROR: ' . $e->getMessage());
}
然而,当我尝试使用
将数据发送到Web服务时try {
$client = new Zend\Soap\Client("http://www.webservicex.net/country.asmx?WSDL");
$result = $client->GetCurrencyByCountry('Australia');
print_r($result);
} catch (SoapFault $s) {
die('ERROR: [' . $s->faultcode . '] ' . $s->faultstring);
} catch (Exception $e) {
die('ERROR: ' . $e->getMessage());
}
我刚收到以下消息
ERROR: [soap:Receiver] System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Data.SqlClient.SqlException: Procedure or function 'GetCurrencyByCountry' expects parameter '@name', which was not supplied. at WebServicex.country.GetCurrencyByCountry(String CountryName) --- End of inner exception stack trace ---
如何向网络服务提供参数?
答案 0 :(得分:6)
您的问题出在请求中,WDSL定义了复杂的类型:
<s:element name="GetCurrencyByCountryResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetCurrencyByCountryResult" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
所以你需要的是构建一个对象或一个assoziative数组,以供webservice使用。对于对象变体,您可以使用stdClass。如果要像这样修改函数调用:
$params = new \stdClass();
$params->CountryName = 'Australia';
$result = $client->GetCurrencyByCountry($params);
您的请求符合类型,数据将发送到服务器。在提供的WDSL中,您需要处理更复杂的变体:
<wsdl:message name="GetISOCountryCodeByCountyNameSoapOut">
<wsdl:part name="parameters" element="tns:GetISOCountryCodeByCountyNameResponse"/>
</wsdl:message>
需要这样的设置:
$params = new \stdClass();
$params->parameters = new \stdClass();
$params->parameters->GetISOCountryCodeByCountyNameResult = 'YOURVALUE';
或作为数组:
$params = array('parameters'=>
array('GetISOCountryCodeByCountyNameResult'=>'VALUE')
);