SOAP 的 PHP 数组/对象结构 (WSDL/wsse)

问题描述 投票:0回答:1
php xml soap wsdl
1个回答
0
投票

问题在于内置 PHP 函数 SoapHeader 需要提供广泛兼容的 SoapHeader。您可以通过扩展 PHP SoapHeader 类来查看实现。

我使用了

stdClass
对象作为有效负载。并通过将所有属性传递到最深的对象层来获取实际的数据字段。我希望通过我的问题的答案为某人节省一些研究。

安全肥皂头:

namespace AppName\TheBundle\Services;

use SoapHeader;
use SoapVar;

class AuthSoapHeaderHelper extends SoapHeader
{
    private $wss_ns = 'http://schemas.xmlsoap.org/.../secext';
    private $username = "username"; 
    private $password = "good_password"; 

    function __construct()
    {

        $auth = new \stdClass();
        $auth->Username = new SoapVar($this->username, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
        $auth->Password = new SoapVar($this->password, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);

        $username_token = new \stdClass();
        $username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns);

        $security_sv = new SoapVar(
            new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns),
            SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);
        parent::__construct($this->wss_ns, 'Security', $security_sv, true);
    }
}

SOAP 有效负载:

$soap_request = new \stdClass();
$soap_request->Payload = new \stdClass();
$soap_request->Payload->Request = new \stdClass();
$soap_request->Payload->Request->Sub = "DATA_1";
$soap_request->Payload->Request->EID = "DATA_2";
$soap_request->Payload->Request->IID = "DATA_3";
$soap_request->Payload->Request->Customer = new \stdClass();
$soap_request->Payload->Request->Lead = new \stdClass();

foreach ($data['x'] as $key => $value)
{
    $soap_request->Payload->Request->Customer->{$key} = $value;
}
foreach ($data['xx'] as $key => $value)
{
    $soap_request->Payload->Request->Lead->{$key} = $value;
}

这导致了以下 wsdl/xml 结构:

<soapenv:Envelope xmlns="http://schemas.xmlsoap.org/1">
    <soapenv:Header>
        <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/3">
         <wsse:UsernameToken xmlns:wsu="http://schemas.xmlsoap.org/4">
          <wsse:Username>username</wsse:Username>
          <wsse:Password Type="wsse:PasswordText">good_password</wsse:Password>
         <wsse:UsernameToken>
        </wsse:Security>    
    </soapenv:Header>

    <soapenv:Body>
      <asi:ProcessMsg>
        <req:Payload>
          <req:Request>
            <req:Sub>DATA_1</req:Sub>
            <req:EID>DATA_2</req:EID>
            <req:IID>DATA_3</req:IID>
            <req:Customer FirstName="xxx" LastName="xxx"></req:Customer>
            <req:Lead LeadMaster="xxx" IntendedRepPer="xxx"></req:Lead>
          </req:Request>
        </req:Payload>
      </asi:ProcessMsg>
    </soapenv:Body>

   </soapenv:Envelope>
© www.soinside.com 2019 - 2024. All rights reserved.