我正在尝试在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 ---

如何为Web服务提供参数?

最佳答案

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

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

10-13 04:44