繁体   English   中英

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

[英]PHP Array/Object Structure for SOAP (WSDL/wsse)

我必须了解如何使用PHP生成此示例WSDL的结构。 (SoapClient,SoapHeaders)

可以说实际输出应如下所示:

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

我尝试了三种都不起作用的结构

  • 新\\ stdClass();
  • 新\\ ArrayObject();
  • array()

“ new SoapHeader();”的结构

 $Security = new \ArrayObject();
 $Security['UsernameToken'] = new \ArrayObject();
 $Security['UsernameToken']['Username'] = "name";
 $Security['UsernameToken']['Password'] = "good_password";

 // OR

 $Security = new \stdClass();
 $Security->UsernameToken = new \stdClass();
 $Security->UsernameToken->Username = "name";
 $Security->UsernameToken->Password = "good_password";


$header = new SoapHeader('http://schemas.xmlsoap.org/ws/2002/07/secext','Security',$Security,false);
$soapClient->__setSoapHeaders($header);

“ $ soap_client-> ServerMethod(“ Payload”,$ soap_request);“的结构:

$soap_request = new \ArrayObject();
$soap_request['Payload'] = new \ArrayObject();
$soap_request['Payload']['Request'] = new \ArrayObject();
$soap_request['Payload']['Request']['Sub'] = "xxx";
$soap_request['Payload']['Request']['EID'] = "xxx";
$soap_request['Payload']['Request']['IID'] = "xxx";
$soap_request['Payload']['Request']['Customer'] = new \ArrayObject();
$soap_request['Payload']['Request']['Customer']['_'] = '';

$soap_request['Payload']['Request']['Lead'] = new \ArrayObject();
$soap_request['Payload']['Request']['Lead']['_'] = '';


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

// OR

$soap_request = new \stdClass();
$soap_request->Request = new \stdClass();
$soap_request->Request->Sub = "xxx";
$soap_request->Request->EID = "xxx";
$soap_request->Request->IID = "xxx";
$soap_request->Request->Customer = new \ArrayObject();
$soap_request->Request->Customer['_'] = '';
$soap_request->Request->Lead = new \ArrayObject();
$soap_request->Request->Lead['_'] = '';
foreach ($data['customer'] as $key => $value)
{
    $soap_request->Request->Customer[$key] = $value;
}
foreach ($data['lead'] as $key => $value)
{
    $soap_request->Request->Lead[$key] = $value;
}

我尝试调试结构的事情:

echo "FNs:\n" . $soapClient->__getFunctions() . "\n";
echo "REQUEST:\n" . $soapClient->__getLastRequest() . "\n";
echo "REQUEST (htmlent):\n" . htmlentities($soapClient->__getLastRequest()) . "\n";

错误消息卡住了:

  • 输入的格式不正确或不包含预期的数据
  • 调用PropertySet.GetChild()失败。 (该属性集没有任何子级。(SBL-EXL-00144))

实际发送执行:

$soap_response = $soapClient->ServerMethod($soap_request);

好吧即时消息真的停留在此刻,甚至不知道要搜索错误/错误,因为我没有任何“好”冗长的错误可以调试。

这是我第一次必须使用SOAP(将数据发送到服务),也许我完全错了吗? 非常感谢您的帮助。

核心问题:

php内部的结构与所有名称空间,属性和嵌套元素如何相像?

问题是内置的php函数SoapHeader没有提供与wsse兼容的Soap-Header。 再往下看,您可以通过扩展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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM