1

I got a SOAP (normal php client) Result from my request I want to save the result as a XML file ? But how ?

   $result = $client->__soapCall('MYwebServices',array($params));

    $xml = simplexml_load_string($result);

    $fp = fopen("out.xml","w");
    fwrite($fp,$xml);
    fclose($fp);
koos
  • 93
  • 1
  • 2
  • 4

3 Answers3

4

If you want to get the XML returned by the web service, you should use SoapClient::__getLastResponse() (along with the trace option).

$client = new SoapClient(..., array('trace' => 1));

// Do your __soapCall here

$xml = $client->__getLastResponse();

// Work with the XML string here
salathe
  • 51,324
  • 12
  • 104
  • 132
  • 1
    Similarly, if you want to get the request XML, use [`SoapClient::__getLastRequest()`](https://www.php.net/manual/en/soapclient.getlastrequest.php). See [this](https://stackoverflow.com/a/3572445/1657502). – Antônio Medeiros Jul 12 '21 at 18:44
1
$xml->asXML("out.xml");

SimpleXMLElement::asXML reference

J0HN
  • 26,063
  • 5
  • 54
  • 85
  • Confirmed that the above does not work ? could be i got a few namespaces in result ? – koos Aug 22 '11 at 12:41
  • Sorry, don't get you. You might need to chec if you SOAP request successful: `is_soap_fault($result)`. Here's manual on [is_soap_fault](http://www.php.net/manual/ru/function.is-soap-fault.php) – J0HN Aug 22 '11 at 12:44
0

Does the code above not do it, if not try:

$result = $client->__soapCall('MYwebServices',array($params));

    $xml = new DOMDocument();
$xml->load($result);
$xml->save("out.xml");

This might be broken if the return is not xml or the xml is improperly formatted, in which case try this:

$result = $client->__soapCall('MYwebServices',array($params));
    libxml_use_internal_errors(true);//load if improperly formatted
        $xml = new DOMDocument();
    if ($xml->load($result))
    {
        $xml->save("out.xml");
    }
    else {
        echo "The return data was not xml";
    }
Liam Bailey
  • 5,879
  • 3
  • 34
  • 46