PHP – Send a SOAP Request by cURL

We can use the PHP cURL library to generate simple HTTP POST request. The following example shows you how to create a simple SOAP request using cURL.

Create the soap-server.php which write the SOAP request into soap-request.xml in web folder.

<?php
  $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  $f = fopen("./soap-request.xml", "w");
  fwrite($f, $HTTP_RAW_POST_DATA);
  fclose($f);
?>

 

The next step is creating the soap-client.php which generate the SOAP request using the cURL library and send it to the soap-server.php URL.

<?php
  $soap_request  = "<?xml version=\"1.0\"?>\n";
  $soap_request .= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">\n";
  $soap_request .= "  <soap:Body xmlns:m=\"http://www.example.org/stock\">\n";
  $soap_request .= "    <m:GetStockPrice>\n";
  $soap_request .= "      <m:StockName>IBM</m:StockName>\n";
  $soap_request .= "    </m:GetStockPrice>\n";
  $soap_request .= "  </soap:Body>\n";
  $soap_request .= "</soap:Envelope>";

  $header = array(
    "Content-type: text/xml;charset=\"utf-8\"",
    "Accept: text/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    "SOAPAction: \"run\"",
    "Content-length: ".strlen($soap_request),
  );

  $soap_do = curl_init();
  curl_setopt($soap_do, CURLOPT_URL, "http://localhost/php-soap-curl/soap-server.php" );
  curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
  curl_setopt($soap_do, CURLOPT_TIMEOUT,        10);
  curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
  curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($soap_do, CURLOPT_POST,           true );
  curl_setopt($soap_do, CURLOPT_POSTFIELDS,     $soap_request);
  curl_setopt($soap_do, CURLOPT_HTTPHEADER,     $header);

  if(curl_exec($soap_do) === false) {
    $err = 'Curl error: ' . curl_error($soap_do);
    curl_close($soap_do);
    print $err;
  } else {
    curl_close($soap_do);
    print 'Operation completed without any errors';
  }
?>

 

Enter the soap-client.php URL in browser to send the SOAP message. If success, Operation completed without any errors will be shown and the soap-request.xml will be created.

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  <soap:Body xmlns:m="http://www.example.org/stock">
    <m:GetStockPrice>
      <m:StockName>IBM</m:StockName>
    </m:GetStockPrice>
  </soap:Body>
</soap:Envelope>

 

Done =)

Reference:

22 thoughts on “PHP – Send a SOAP Request by cURL”

  1. Thank you very much. I just had some trouble with native SoapClient objects and I decided to send a raw xml message. Your post has helped me a lot.

    Like

  2. Thanks very much for this post. It has been very informative!
    I tried this code to send a raw SOAP request using authentication but all I get back from the server is the full wsdl file for the service. Does this method handle authentication as well over https?

    Thanks again,

    JB

    Like

  3. Been trying to send through soapClient for over 2 weeks now. After deceiding to send it straight as an xml, I found your blog. Fixed the problem within 10 minutes.
    Extreme simple solution, which gets the job done.
    Maximus Kudos!

    Like

  4. I didn’t know SOAP but a job dropped on my lap that needed integration of a SOAP service without WDSL, which most other tutorials use, I was scratching my head as the service’s manual only provided the XML, no URI or WDSL link. Thanks for sharing your expertise, I totally know how to continue now, thanks!

    Like

  5. Hello I tried this and it all worked till the output data because I cant find it.
    After testing the example above I tried adapting it to my SOAP site 🙂 but cant get it to work.
    If possible would anyone please help me out?
    I want to do a zipcode check for a database (postcodecheck).
    https://pqcc.soap.dslorder.nl/pqcc/v7.0/pqcc.asmx
    for the test you could use PostalCode=”1213xd” HouseNumber=”42″ HouseNumberAddition=”a”

    I really need you’re help

    Like

      1. Hi Thanks for your reply. I haven’t had time to test this because of my work. But am I understanding this correctly that I have to add the entire thing so the above xml and the below xml into my PHP script? Sorry for not understanding, i’m new to all of this and trying to find out .

        Like

      2. Hello ykyuen,
        First of all thanks for your reply long ago 🙂 .
        Because of other projects I had to put all of this on hold.
        At the moment I havent tried my own SOAP site yet, because I’m trying to get your instructions to work.

        Everything works from creating the soap-server.php and soap-client.php and when I run the client I get a “Operation completed without any errors” sign. However, when I look at the xml output it is blanc. Do you also have this problem?

        Like

  6. I;ve found the problem I had to change the first line of the Curl’s URL. Now I’ll have to adapt this to my SOAP witch is quite hard. Do you mind if I ask you for help if I have problems?

    Like

    1. Thanks. I solved the issue. $HTTP_RAW_POST_DATA feature was DEPRECATED in PHP 5.6.0, and REMOVED as of PHP 7.0.0.

      Like

  7. This method has the potential of being very useful since my wsdl/curl request has a long xml to do the query. All other soap methods seem to be too simple for a long xml soap request. I’m not seeing where to get the result of the query back – not success or failure. Any thoughts? Where is the query result after doing the above? Thanks!

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.