0

I am trying to write some code to connect a webpage to an external API. The API's documentation tells me that the request should be done with a SOAP envelope, which should look like this:

<soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema">
<soap:Body>
  <GetNote>
    <Request>
      <Security>
        <Username>username</Username>
        <Password>password</Password>
      </Security>
      <NoteID>noteID</NoteID>
    </Request>
  </GetNote>
</soap:Body>
</soap:Envelope>

Using some sample code created by a helpful StackOverflow user here: SOAP request in PHP with CURL I then turned it into this (slightly redacted) php:

<?php
$soapUrl = 'https://www.blah.com/DoodadService.cfc?wsdl';
$xml_post_string = '<soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema">
<soap:Body>
  <GetNote>
    <Request>
      <Security>
        <Username>'.$soapUser.'</Username>
        <Password>'.$soapPassword.'</Password>
      </Security>
      <NoteID>'.$soapNoteID.'</NoteID>
    </Request>
  </GetNote>
</soap:Body>
</soap:Envelope>';

$headers = array(
                        "Content-type: text/xml;charset=\"utf-8\"",
                        "Accept: text/xml",
                        "Cache-Control: no-cache",
                        "Pragma: no-cache",
                        "SOAPAction: https://www.blah.com/DoodadService.cfc?wsdl",
                        "Content-length: ".strlen($xml_post_string),
                    );

$url = $soapUrl;


$ch = curl_init();
echo('set curl<br>');
$f = fopen('request.txt','w');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE,true);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo('about to execute<br>');
// converting
$response = curl_exec($ch);
fclose($f);
echo('Got '.strlen($response).' characters in response<br>');
curl_close($ch);
echo('Saved error strings to request.txt<br> ');
?>

The result was a Version Mismatch error, ie:

<soapenv:Fault>
<faultcode>
soapenv:VersionMismatch</faultcode>
<faultstring>
Version Mismatch</faultstring>
<detail>
<ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">
ip-10-4-4-149</ns1:hostname>
</detail>
</soapenv:Fault>

Can anyone tell me what this error means, what is being "mismatched" against what, and how I might be able to fix it up?

Thanks!

Community
  • 1
  • 1
  • 1
    It's an error from the web service. It expects some parameters in the header that are not defined, or they have a wromg format. Do you have access to the service documentation for that action? – F.Igor Dec 16 '16 at 20:27
  • No, "this is what the soap envelope looks like" and "this is the URL of the service" are the sum total of information available to me at the moment. I did send off a query to the API provider, but I'm not expecting a response for at least a couple of days (and given Christmas, who knows...) so I thought I'd see if there any obvious flaws to anyone who _generally_ knows about soap – Emma Baillie Dec 18 '16 at 02:34

2 Answers2

0

You could try to change the Content-type header to a SOAP+XML content. The charset doesn't need quotation.

"Content-type: application/soap+xml; charset=utf-8"

Otherwise, surely you have missed something that you don't know. You could find some examples using this API or similar cases with SOAP in another SO questions.

F.Igor
  • 4,119
  • 1
  • 18
  • 26
0

As it turns out, the issue was "online manual giving out of date information". Not a problem that general SOAP experts can help out with after all!

In course of consultation with the API providers, was pointed at a useful tool SoapUI - https://www.soapui.org/ - which, as it turns out, can be used to generate correct Soap envelopes given the API's URL - meaning that I am no longer dependent on this API's providers to have their documentation up to date. I recommend it to anyone else who finds themselves in a similar fix.