2

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

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

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

如何为 web 服务提供参数?

4

1 回答 1

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>

因此,您需要构建一个对象或关联数组以供 Web 服务使用。对于对象变体,您可以使用 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')
);
于 2013-01-28T03:11:39.400 回答