70

Since the SOAP manual on php.net is not very noob friendly and I could not find any good examples I will post my question here.

How can I create PHP SOAP request to look like this?

POST /MySERVER/myWSDLservice.asmx HTTP/1.1
Host: connection.mywebsite.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://connection.mywebsite.com/MySERVER/GetCarType"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
  <GetCarType xmlns="http://connection.mywebsite.com/MySERVER/">
    <IDNumber>string</IDNumber>
  </GetCarType>
 </soap:Body>
</soap:Envelope>

Please note:

  • there is user/pass auth
  • SSL connection

Any suggestion / links / example much appreciated.

Simon
  • 105
  • 4
Iladarsda
  • 10,250
  • 38
  • 99
  • 165

1 Answers1

171

Tested and working!

  • with https, user & password

     <?php 
     //Data, connection, auth
     $dataFromTheForm = $_POST['fieldName']; // request data from the form
     $soapUrl = "https://connecting.website.com/soap.asmx?op=DoSomething"; // asmx URL of WSDL
     $soapUser = "username";  //  username
     $soapPassword = "password"; // password
    
     // xml post structure
    
     $xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
                         <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                           <soap:Body>
                             <GetItemPrice xmlns="http://connecting.website.com/WSDL_Service"> // xmlns value to be set to your WSDL URL
                               <PRICE>'.$dataFromTheForm.'</PRICE> 
                             </GetItemPrice >
                           </soap:Body>
                         </soap:Envelope>';   // data from the form, e.g. some ID number
    
        $headers = array(
                     "Content-type: text/xml;charset=\"utf-8\"",
                     "Accept: text/xml",
                     "Cache-Control: no-cache",
                     "Pragma: no-cache",
                     "SOAPAction: http://connecting.website.com/WSDL_Service/GetPrice", 
                     "Content-length: ".strlen($xml_post_string),
                 ); //SOAPAction: your op URL
    
         $url = $soapUrl;
    
         // PHP cURL  for https connection with auth
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
         curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
         curl_setopt($ch, CURLOPT_TIMEOUT, 10);
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
         // converting
         $response = curl_exec($ch); 
         curl_close($ch);
    
         // converting
         $response1 = str_replace("<soap:Body>","",$response);
         $response2 = str_replace("</soap:Body>","",$response1);
    
         // convertingc to XML
         $parser = simplexml_load_string($response2);
         // user $parser to get your data out of XML response and to display it. 
     ?>
    
sauhardnc
  • 1,798
  • 2
  • 4
  • 16
Iladarsda
  • 10,250
  • 38
  • 99
  • 165
  • 1
    The script works great and return XML SOAP response pastebin.com/9wzUV8Pw but I'm unable to parse the response into string, Any idea? – Irfan Aug 14 '13 at 11:39
  • Without testing it the simplexml_load_string() cant handle the ":", right? – whereismydipp Aug 21 '13 at 08:50
  • I'm trying to do consume a SOAP webservice using curl, but I only get an error that says "Unsupported Media Type". There is a way to force the Content-type header? When I set CURLOPT_POSTFIELDS the Content-type changes to "application/x-www-form-urlencoded". – Gustavo Straube Mar 26 '14 at 20:46
  • 1
    I found the problem here! I'm writing the headers using associative array. I only noted that when read this answer: http://stackoverflow.com/a/11091261/1128918 – Gustavo Straube Mar 26 '14 at 21:14
  • This, as of February 2017, still works as intended and helps people when they got headache while working with SOAP. I'd only like to point out the fact that, some servers, requires a prefix (like ``) for the xml tags (you can see that when the response contains a message about an unidentified sub-element) – Erenor Paz Feb 20 '17 at 15:35
  • This code sample works wonders, but if nstead of replacing the whole like this str_replace("","",$response); you remove all the soap tags like this: str_replace("soap:", "", $response); It will also take care of the errors in the response, the "Fault", and it'll work for all types of responses. – Yuleidy Jul 30 '18 at 21:08
  • @lladarsda can you please my [question](https://stackoverflow.com/questions/55980022/unable-to-convert-soap-response-string-to-xml-in-yii2) related to it ? – Moeez May 09 '19 at 08:05
  • and if the script do not work: `curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); ` `0` in stead of `1` ignores problems with the SSL certificate. In my case it was the solution. – Sarah Trees Mar 07 '20 at 12:40