Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

PHP soap request with an element attribute and child elements

Below is my code to construct the soap request. I can create a client, but to update a client, I need the client number as an attribute in the client tag.

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

This produces:

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

But what I need is all of that above, and an attribute in the Client tag with all of the child elements:

<urn:Client Attr1='XXXX1121111'>

I went through all of the posts that I could find in SO. I cannot produce an attribute to element that has child elements. I have tried using XSD_ANYXML, and built a view to insert into a SoapVar(), and it did not work, because I needed the namespaces. This is most likely a repeat of another question, but those have been unanswered.

Below are just some of the posts I have been to on this issue:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was trying to build the Soap request through SoapVar. Trying to nest elements into elements. Instead of doing that I simply created an associative array in the same structure as the request needed to be in. I.E.

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

Which produced the soap request I was looking to get. I removed all of the soapvar elements, and just used the array as a parameter in the function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...