gpt4 book ai didi

php - 带有 Zend 框架 2 的 SOAP 客户端

转载 作者:行者123 更新时间:2023-12-01 17:17:43 25 4
gpt4 key购买 nike

我正在尝试在 Zend Framework 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());
}

然而,当我尝试将数据发送到网络服务时,例如使用

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 ---

如何向网络服务提供参数?

最佳答案

您的问题在请求中,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>

因此,您需要构建一个对象或关联数组,以供网络服务使用。对于对象变体,您可以使用 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')
);

关于php - 带有 Zend 框架 2 的 SOAP 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14553873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com