SOAP

时间:2017-10-05 07:51:12

标签: php web-services soap

我正在尝试创建SOAP请求并尝试使用SoapClientZend\Soap\Client,但我似乎无法正确构建它。这是应该看起来的信封:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
   xmlns:tem="http://tempuri.org/" 
   xmlns:imag="http://custom-url-here">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:Logon>
         <tem:request>
            <imag:Password>XXXXXXXX</imag:Password>
            <imag:User>XXXXXXXX</imag:User>
         </tem:request>
      </tem:Logon>
   </soapenv:Body>
</soapenv:Envelope>

我没有使用SOAP的经验(REST&amp; JSON踢它的屁股,但无论如何),但这是我迄今为止所管理的:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns1="http://tempuri.org/" 
    xmlns:ns2="custom-url-here">

    <SOAP-ENV:Header>
        <ns2:imag/>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>

    <ns1:Logon>
        <request>
            <Password>XXXXXX</Password>
            <User>XXXXXXX</User>
        </request>
    </ns1:Logon>

    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

正如你所看到的,这是错误的,但它已经到了那里。这是我使用的代码:

$wsdl = 'http://url-of-wsdl';
$namespace = 'http://custom-url-here';

$client = new Client($wsdl);
$client->setSoapVersion(SOAP_1_1);

$header = new \SoapHeader($namespace, 'imag');
$client->addSoapInputHeader($header);

$params = [
    new \SoapVar([
        new \SoapVar($password, XSD_STRING, null, $namespace, 'Password'),
        new \SoapVar($username, XSD_STRING, null, $namespace, 'User'),
     ], SOAP_ENC_OBJECT, null, $namespace, 'request'),
];
$result = $client->Logon(new SoapVar($params, SOAP_ENC_OBJECT));

有人可以指出我做错了什么吗?正如我所说,我通常使用JSON REST接口,所以可能有几个问题! 谢谢! : - )

1 个答案:

答案 0 :(得分:0)

好的,我已经设法让它返回我需要的回复!我改变了这些路线:

$params = [
            new \SoapVar([
                new \SoapVar($password, XSD_STRING, null, null, 'ns2:Password'),
                new \SoapVar($username, XSD_STRING, null, null, 'ns2:User'),
            ], SOAP_ENC_OBJECT, null, null, 'ns1:request'),
        ];

它有效,但生成的信封仍然看起来不像原版,所以这不是最好的答案,如果有人能解释如何让它相同,我将非常高兴!

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:ns1="http://tempuri.org/"
                   xmlns:ns2="http://my-custom-url">
    <SOAP-ENV:Header>
        <ns2:imag/>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:Logon>
            <ns1:request>
                <ns2:Password>XXXXXX</ns2:Password>
                <ns2:User>XXXXXX</ns2:User>
            </ns1:request>
        </ns1:Logon>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>