简体   繁体   中英

PHP - Generating correct SOAP data for Salesforce API

Can anyone help point me in the correct direction to generate valid XML to use with the Salesforce API. I normally avoid SOAP like the plague but have no choice here and it seems like I'm at the bottom of a vertical learning curve.

I've got a full WSDL which as far as I'm aware should cover every part of the request, including all the appropriate namespaces and complex types, but I just can't generate a request that matches their example.

A subset of the example request looks like the following -

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://soap.sforce.com/schemas/class/WebsiteAPI" xmlns:web1="http://soap.sforce.com/schemas/class/WebsiteAPIUtils">
   <soapenv:Header>
      <web:SessionHeader>
         <web:sessionId> SESSION_ID_FROM_LOGIN_METHOD </web:sessionId>
      </web:SessionHeader>
   </soapenv:Header>
   <soapenv:Body>
      <web:upsertLeadOrUpdateAccount>
       <web:leadFromWebsite>
            <!--Optional:-->
            <web1:salesforceId></web1:salesforceId>
            ...

By calling the relevant method I can get an <ns1:upsertLeadOrUpdateAccount> request (not the same namespace id but that doesn't really matter), with an empty <leadFromWebsite> element. However, I just can't work out how to get the <salesforceId> sub element in there with the correct namespace. Everything I do either seems to do nothing, or adds an "xsi-type" attribute to the parent rather than using the namespace prefix.

The closest I've got is the following, which adds the namespace to the root element, but seems to create a weird looking xsi-type="ns1:web1" attribute, rather than just prefixing all the elements with web1 .

I do have complexTypes for everything in the WSDL so I'm sure there should be a way for the SOAP library to handle all the heavy lifting for me. I really don't want to resort to just generating the whole lot by hand.

$data = new stdclass();
$data->salesforceId = '123';

$args = [

    'leadFromWebsite' => new SoapVar($data, SOAP_ENC_OBJECT, 'web1', 'http://soap.sforce.com/schemas/class/WebsiteAPIUtils')
];

Output -

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:ns1="http://soap.sforce.com/schemas/class/WebsiteAPIUtils" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:ns2="http://soap.sforce.com/schemas/class/WebsiteAPI">
  <SOAP-ENV:Header>
    <ns2:SessionHeader>
      <ns2:sessionId>123</ns2:sessionId>
    </ns2:SessionHeader>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns2:upsertLeadOrUpdateAccount>
      <ns2:leadFromWebsite xsi:type="ns1:web1">
        <salesforceId>123</salesforceId>
      </ns2:leadFromWebsite>
    </ns2:upsertLeadOrUpdateAccount>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I haven't tested this fully with the salesforce API yet, but I think I've managed to generate XML that looks correct - at least a lot closer than I was.

Seems my main issue was confusing type and node namespaces. I wanted to specify the namespace for the node, not the data type.

The following code generates xml that is much closer to the example requests -

$data = [];
$data[] = new SoapVar('123', XSD_STRING, null, null, 'salesforceId', 'http://soap.sforce.com/schemas/class/WebsiteAPIUtils');

$args = [
    'leadFromWebsite' => new SoapVar($data, SOAP_ENC_OBJECT, null, null, 'leadFromWebsite', 'http://soap.sforce.com/schemas/class/WebsiteAPI')
];

$s->upsertLeadOrUpdateAccount($args);

XML -

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:ns1="http://soap.sforce.com/schemas/class/WebsiteAPIUtils" 
  xmlns:ns2="http://soap.sforce.com/schemas/class/WebsiteAPI">
  <SOAP-ENV:Header>
    <ns2:SessionHeader>
      <ns2:sessionId>123</ns2:sessionId>
    </ns2:SessionHeader>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns2:upsertLeadOrUpdateAccount>
      <ns2:leadFromWebsite>
        <ns1:salesforceId>123</ns1:salesforceId>
      </ns2:leadFromWebsite>
    </ns2:upsertLeadOrUpdateAccount>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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