Link to home
Start Free TrialLog in
Avatar of Jeremy Leys
Jeremy LeysFlag for New Zealand

asked on

Post xml via PHP Soap Client

We are trying to post the xml input of a successful SoapUI call to the same endpoint, via XML.  The endpoint is a NTLM authenticated .net service.  We are successfully logging into this from PHP but we cannot send an object based on valid xml from SoapUI via a PHP xml object.

So

1)    Should we be able to post the xml directly to this service from PHP?
2)    If not, can we generate valid paramaters for the soap function from our valid xml?

A sample snippet of xml is

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:spo="urn:xxxxx" xmlns:sal="urn:xxxx" xmlns:val="spoint/edi/validationresults">
   <soapenv:Header/>
   <soapenv:Body>
      <spo:SendSalesDocument>
         <spo:document>
            <sal:ProcessorCode>INBOUND</sal:ProcessorCode>
            <sal:HandlerCode>MAG.SALES.ORDER</sal:HandlerCode>
            <sal:ReferenceNo>TEST</sal:ReferenceNo>
            <sal:DocumentType>ORDER</sal:DocumentType>
            <sal:SellToCustomer>
               <sal:CustomerNo>10002</sal:CustomerNo>
               <sal:Name>Sample Magento Sale</sal:Name>
               <sal:Address>Sample Address 1</sal:Address>
               <sal:Address2>Sample Address 2</sal:Address2>
               <sal:City>Sydney</sal:City>
               <sal:PostCode>2000</sal:PostCode>
               <sal:County>NSW</sal:County>
               <sal:CountryCode>AU</sal:CountryCode>
            </sal:SellToCustomer>
            <sal:ExternalDocumentNo>MAG-000001</sal:ExternalDocumentNo>
            <sal:DocumentDate>2016-12-01</sal:DocumentDate>
            <sal:RequestedDeliveryDate>2016-12-07</sal:RequestedDeliveryDate>
            <sal:PaymentMethodCode>EFT</sal:PaymentMethodCode>
            <sal:ShipmentMethodCode>CP</sal:ShipmentMethodCode>
            <sal:SalesLine>
               <sal:SequenceNo>10000</sal:SequenceNo>
               <sal:Type>ITEM</sal:Type>
               <sal:No>11100</sal:No>
               <sal:Description>MitoQ Cleanser 150ml</sal:Description>
               <sal:Quantity>1</sal:Quantity>
               <sal:UnitPrice>10</sal:UnitPrice>
            </sal:SalesLine>
         </spo:document>
         <spo:validationResult>
            <val:Message>
               <val:MessageNo>0</val:MessageNo>
               <val:Status></val:Status>
               <val:WorkflowStatus></val:WorkflowStatus>
               <val:Severity></val:Severity>
               <val:NoOfEntries></val:NoOfEntries>
               <val:Entry>
                  <val:MessageLineNo>0</val:MessageLineNo>
                  <val:LineNo>0</val:LineNo>
                  <val:SequenceNo></val:SequenceNo>
                  <val:Severity></val:Severity>
                  <val:FieldNo>0</val:FieldNo>
                  <val:FieldName></val:FieldName>
                  <val:RuleCode></val:RuleCode>
                  <val:RuleDescription></val:RuleDescription>
                  <val:CreatedDateTime>0001-01-01T00:00:00</val:CreatedDateTime>
                  <val:Text></val:Text>
               </val:Entry>
            </val:Message>
         </spo:validationResult>
      </spo:SendSalesDocument>
   </soapenv:Body>
</soapenv:Envelope>

Open in new window


Thanks, Martin
Avatar of Jeremy Leys
Jeremy Leys
Flag of New Zealand image

ASKER

Sample extension of PHP SoapClient class which is successfully authenticating but not posting valid xml:

$xml = simplexml_load_string($_sample);

var_dump($_client->__doRequest($xml, 'https://uri', 'SendSalesDocument', '', $one_way = false));
var_dump($_client->__getLastRequest());
class NtlmClient extends SoapClient
{
    protected $options;


    public function __construct($url, $options = [])
    {
        $this->options = $options; // so we can access the credentials
        parent::__construct($url, $options);
    }


    public function __doRequest($request, $location, $action, $version, $one_way = false)
    {
        $this->__last_request = $request;

        $handle = curl_init($location);

        $credentials = $this->options['login'] . ':' . $this->options['password'];
        $headers = [
            'Method: POST',
            'User-Agent: PHP-SOAP-CURL',
            'Content-Type: text/xml; charset=utf-8',
            'SOAPAction: "' . $action . '"'
        ];

        curl_setopt($handle, CURLINFO_HEADER_OUT, true);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($handle, CURLOPT_POST, true);
        curl_setopt($handle, CURLOPT_POSTFIELDS, $request);
        curl_setopt($handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 2);

        // Authentication
        curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
        curl_setopt($handle, CURLOPT_USERPWD, $credentials);

        $response = curl_exec($handle);

        return $response;
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Shalom Carmel
Shalom Carmel
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you your example helped me immensely and I now have a working solution :)