cURL with SOAP headers.

Posted on 2007-09-28
Last Modified: 2013-11-18
Hi, I am sending xml using cURL and today I ran into SOAP requirement for the first time. rather then implement something like nuSOAP and save myself some time. Is there any way to add the SOAP Elements using cURL.  Below is the soap header requirements i need to include somehwo and below that is a snippet of my code. . without the xml, the xml is stored as $xml_request.

POST /dfswebsrvc/dfswebsrvc.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: ""

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap="">
    <InsertOrder xmlns="">

// MY CODE THUS FAR - i send the request then store the response to a tmp directory

          /* Use CURL to execute XML POST and write output to the temp file */
          $ch = curl_init (GATEWAY_URL);
          curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt ($ch, CURLOPT_POST, 1);
          curl_setopt ($ch, CURLOPT_POSTFIELDS, $xml_request);
          curl_setopt ($ch, CURLOPT_FILE, $fh);

          /* Read data from file into a string buffer */
          rewind ($fh);
          while (!feof($fh)) {
              $xml_response .= fgets($fh, 1024);

          $this->parser = xml_parser_create();

          /* Disable XML tag capitalisation (Case Folding) */
          xml_parser_set_option ($this->parser, XML_OPTION_CASE_FOLDING, FALSE);

          /* Define Callback functions for XML Parsing */
          xml_set_object($this->parser, &$this);
          xml_set_element_handler ($this->parser, "ep_xml_element_start", "ep_xml_element_end");
          xml_set_character_data_handler ($this->parser, "ep_xml_data");

          /* Parse the XML response */
          xml_parse($this->parser, $xml_response, TRUE);
                                                      //      exit();
          /* Clean up after ourselves */
          xml_parser_free ($this->parser);
          fclose ($fh);

Question by:redcable
    LVL 12

    Accepted Solution

    Before you execute this line:
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $xml_request);

     change the $xml_request to have the soap layer. Why not just prepend and append the wrapper onto it? Eg something like:

    $xml_request = "<?xml version="1.0" encoding="utf-8"?>" +
      "<soap:Envelope xmlns:xsi=\"\"" +
        " xmlns:xsd=\"\"" +  
        "xmlns:soap=\"\">" +
        "<soap:Body>" +
          $xml_request +
        " </soap:Body>" +

    Author Comment

    when i try that i get this

    Unable to handle request without a valid action parameter. Please supply a valid soap action.
    LVL 20

    Expert Comment

      did you ever figure out the answer to this?  I am experiencing an identical outcome for sending a SOAP request via cURL.
    LVL 12

    Expert Comment

    My guess is the <soap:Header> was missing.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
    SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now