带有元素属性和子元素的PHP soap请求

时间:2016-06-01 16:03:48

标签: php soap attributes element

以下是构建soap请求的代码。我可以创建一个客户端,但是要更新客户端,我需要客户端编号作为客户端标记中的属性。

$urn = '<SOME DEFINITION>';
$urn1 = '<SOME DEFINITION>';
$urn2 = '<SOME DEFINITION>';

// Main descriptive content of the client
$clientType = $clientInfo['clt_type'];
$companyName = $clientInfo['clt_name'];
$mainPaymentMethod = '0001';
$mainPaymentCondition = '0003';

/* Build sales portion */
$sales_city = $clientInfo['clt_sales_city'];
$sales_area1 = $clientInfo['clt_sales_state'];
$sales_zipCode = $clientInfo['clt_sales_zipcode'];
$sales_countryCode = $clientInfo['clt_sales_country'];
$sales_address = $clientInfo['clt_sales_address'];
$sales_address_two = $clientInfo['clt_sales_address2'];

$salesGeneralFitness = 'MAIN';
$salesBillingFitness = 'MAIN';

$salesAddressParams[] = new SoapVar($sales_address, XSD_STRING, null, $urn1, 'Address', $urn1);
$salesAddressParams[] = new SoapVar($sales_address_two, XSD_STRING, null, $urn1, 'AddressComplement', $urn1);
$salesAddressParams[] = new SoapVar($sales_zipCode, XSD_STRING, null, $urn1, 'ZipCode', $urn1);
$salesAddressParams[] = new SoapVar($sales_city, XSD_STRING, null, $urn1, 'City', $urn1);
$salesAddressParams[] = new SoapVar($salesGeneralFitness, XSD_STRING, null, $urn2, 'GeneralFitness', $urn2);
$salesAddressParams[] = new SoapVar($salesBillingFitness, XSD_STRING, null, $urn2, 'BillingFitness', $urn2);
$salesAddressParams[] = new SoapVar($sales_area1, XSD_STRING, null, $urn2, 'Area1', $urn2);
$salesAddressParams[] = new SoapVar($sales_countryCode, XSD_STRING, null, $urn2, 'CountryCode', $urn2);

$SalesAddress = new SoapVar($salesAddressParams, SOAP_ENC_OBJECT, null, $urn2, 'Address', $urn2);
/* End of sales portion */

/* Build financial portion */
$financial_city = $clientInfo['clt_financial_city'];
$financial_area1 = $clientInfo['clt_financial_state'];
$financial_zipCode = $clientInfo['clt_financial_zipcode'];
$financial_countryCode = $clientInfo['clt_financial_country'];
$financial_address = $clientInfo['clt_financial_address'];
$financial_address_two = $clientInfo['clt_financial_address2'];

$financialGeneralFitness = 'MAIN';
$financialBillingFitness = 'MAIN';

$financialAddressParams[] = new SoapVar($financial_address, XSD_STRING, null, $urn1, 'Address', $urn1);
$financialAddressParams[] = new SoapVar($financial_address_two, XSD_STRING, null, $urn1, 'AddressComplement', $urn1);
$financialAddressParams[] = new SoapVar($financial_zipCode, XSD_STRING, null, $urn1, 'ZipCode', $urn1);
$financialAddressParams[] = new SoapVar($financial_city, XSD_STRING, null, $urn1, 'City', $urn1);
$financialAddressParams[] = new SoapVar($financialGeneralFitness, XSD_STRING, null, $urn2, 'GeneralFitness', $urn2);
$financialAddressParams[] = new SoapVar($financialBillingFitness, XSD_STRING, null, $urn2, 'BillingFitness', $urn2);
$financialAddressParams[] = new SoapVar($financial_area1, XSD_STRING, null, $urn2, 'Area1', $urn2);
$financialAddressParams[] = new SoapVar($financial_countryCode, XSD_STRING, null, $urn2, 'CountryCode', $urn2);

$FinancialAddress = new SoapVar($financialAddressParams, SOAP_ENC_OBJECT, null, $urn2, 'Address', $urn2);

/* Build The rest of the required (not used) soap request */

$LogInfo = new SoapVar('', XSD_STRING, null, $urn2, 'LogInfo', $urn2);
$CommonName = $StartDate = new SoapVar($companyName, XSD_STRING, null, $urn2, 'CommonName', $urn2);
$CompanyName = $StartDate = new SoapVar($companyName, XSD_STRING, null, $urn2, 'CompanyName', $urn2);
$AreImplicitMandatesOk = new SoapVar('false', XSD_STRING, null, $urn2, 'AreImplicitMandatesOk', $urn2);
$ParentInfo = new SoapVar('', XSD_STRING, null, $urn2, 'ParentInfo', $urn2);
$UserProperties = new SoapVar('', XSD_STRING, null, $urn2, 'UserProperties', $urn2);
$StartDate = new SoapVar(date('Y-m-d', time()), XSD_STRING, null, $urn2, 'StartDate', $urn2);
$MainPaymentMethod = new SoapVar($mainPaymentMethod, XSD_STRING, null, $urn2, 'MainPaymentMethod', $urn2);
$MainPaymentCondition = new SoapVar($mainPaymentCondition, XSD_STRING, null, $urn2, 'MainPaymentCondition', $urn2);
$ClientType = new SoapVar($clientType, XSD_STRING, null, $urn2, 'ClientType', $urn2);
$MandateInfo = new SoapVar('', XSD_STRING, null, $urn2, 'MandateInfo', $urn2);
$VATInfo = new SoapVar('', XSD_STRING, null, $urn2, 'VATInfo', $urn2);

/* Set the body content */
$Body[] = $LogInfo;
$Body[] = $CommonName;
$Body[] = $CompanyName;
$Body[] = $StartDate;
$Body[] = $MainPaymentMethod;
$Body[] = $MainPaymentCondition;
$Body[] = $ClientType;
$Body[] = $SalesAddress;
$Body[] = $FinancialAddress;
$Body[] = $AreImplicitMandatesOk;
$Body[] = $ParentInfo;
$Body[] = $UserProperties;
$Body[] = $MandateInfo;
$Body[] = $VATInfo;

   /* Build entire soap request here */
$data = new SoapVar($Body, SOAP_ENC_OBJECT, null, $urn, 'Client', $urn);
$parameters['Client'] = $data;

这会产生:

     <urn:Client> <- Add attribute to this tag
        <urn2:LogInfo>
        </urn2:LogInfo>
        <urn2:CommonName>Some Value</urn2:CommonName>
        <urn2:CompanyName>Some Value</urn2:CompanyName>
        <urn2:StartDate>2016-01-01</urn2:StartDate>
        <urn2:MainPaymentMethod>0001</urn2:MainPaymentMethod>
        <urn2:MainPaymentCondition>0003</urn2:MainPaymentCondition>
        <urn2:ClientType>XXX</urn2:ClientType>
        <urn2:Address>
           <urn1:ZipCode>55555</urn1:ZipCode>
           <urn1:City>City</urn1:City>
           <urn2:GeneralFitness>MAIN</urn2:GeneralFitness>
           <urn2:BillingFitness>MAIN</urn2:BillingFitness>
           <urn2:Area1>NY</urn2:Area1>
           <urn2:CountryCode>US</urn2:CountryCode>
        </urn2:Address>
        <urn2:AreImplicitMandatesOk>false</urn2:AreImplicitMandatesOk>
        <urn2:ParentInfo urn1:dataOmitted="true">
        </urn2:ParentInfo>
        <urn2:UserProperties urn1:dataOmitted="true">
        </urn2:UserProperties>
        <urn2:MandateInfo urn1:dataOmitted="true">
        </urn2:MandateInfo>
        <urn2:VATInfo>
        </urn2:VATInfo>
     </urn:Client>

但我需要的是以上所有内容,以及Client标记中包含所有子元素的属性:

<urn:Client Attr1='XXXX1121111'>

我浏览了所有可以在SO中找到的帖子。我不能为具有子元素的元素生成属性。我已经尝试使用XSD_ANYXML,并构建了一个插入SoapVar()的视图,但它不起作用,因为我需要命名空间。这很可能是另一个问题的重复,但那些问题没有答案。

以下是我在此问题上的一些帖子:

SOAP request with PHP

Create SOAP request with element attribute using Zend_Soap_Client and stdObject

Send other attributes with xsi:type attribute in Soap Request PHP

SOAP Request from PHP is not working

1 个答案:

答案 0 :(得分:0)

我试图通过SoapVar构建Soap请求。试图将元素嵌入元素中。而不是这样做,我只是在与请求所需的结构相同的结构中创建了一个关联数组。

$client->createClient(array('Client' => // Outer most element
       // Begin the list of child elements
       array('AttributeOfOuterElement' => 'AttrValue',
              'ChildElement1' => 'Child1Value' // ..... etc
             )
          );

这产生了我想要的肥皂要求。我删除了所有soapvar元素,并将该数组用作函数中的参数。