45

The question: Is there a way to view the XML that would be created with a PHP SoapClient function call BEFORE you actually send the request?

background:

I am new to WSDL communication, and I have a client who wants me to develop in PHP, a way to communicate with a WSDL service written in ASP.NET. I have gotten pretty far, but am running into an issue when it comes to passing a complex type. I have tried a couple of different things so far.

1) Setting up a single array such as $params->Person->name $params->Person->address

2) Setting up a single array $Person = array('name'=>"joe",'address' = "123");

then passing into the call as a param "Person" => $Person; and a few others. But every time I get the error

SoapException: Server was unable to process request ---> System.Exception: Person is Required. at service name.

In order to further the troubleshooting, I would like to see the XML document that is being sent to see if it is creating a complex type in the way I am expecting it to. I am creating the service using $client = new SoapClient('wsdldoc.asmx?WSDL'); calling it with $client->CreateUser($params); and then trying to see it using the function $client->__getLastRequest(); but it never makes it to the __getLastRequest because it hits a fatal error when calling CreateUser($params).

The question again: Is there any way to view the XML created by the CreateUser($params) call WITHOUT actually sending it and causing a fatal error

hakre
  • 193,403
  • 52
  • 435
  • 836
Joshua Cook
  • 939
  • 2
  • 8
  • 13

3 Answers3

87

Upfront remark: In order to use the __getLastRequest() method successfully, you have to set the 'trace' option to true on client construction:

$client = new SoapClient('wsdldoc.asmx?WSDL', array('trace' => TRUE));

This way, your request will still be sent (and therefore still fail), but you can inspect the sent xml afterwards by calling $client->__getLastRequest().


Main answer:

To get access to the generated XML before/without sending the request, you'd need to subclass the SoapClient in order to override the __doRequest() method:

class SoapClientDebug extends SoapClient
{
  public function __doRequest($request, $location, $action, $version, $one_way = 0) {
      // Add code to inspect/dissect/debug/adjust the XML given in $request here

      // Uncomment the following line, if you actually want to do the request
      // return parent::__doRequest($request, $location, $action, $version, $one_way);
  }
}

You'd then use this extended class instead of the original SoapClient while debugging your problem.

Henrik Opel
  • 19,341
  • 1
  • 48
  • 64
  • I did have trace set to true, but still, for some reason, the script dies after the call is made. Can't even do a $client->createuser();followed by an echo "finished calling"; It never goes past the call. Your main answer however was EXACTLY what I was looking for. Thank you. I'm finding now that NONE of my params are being passed in the XML document for some reason. maybe I'm not calling the function correctly or something. . . . I'll keep looking. Thanks again. – Joshua Cook Oct 26 '09 at 19:31
  • never thought of overriding the __doRequest. this allowed me to see what I needed to fix similar issue. – cromestant Aug 14 '13 at 17:39
  • 1
    The SoapClient throws an exception when it fails. You might need to wrap the call in a try/catch block to call `__getLastRequst()` afterwards. – mAAdhaTTah Jun 29 '15 at 21:57
  • That a really helpful answer, thank you Henrik! – TomiL Apr 02 '21 at 17:14
9

I found this thread while working on the same problem, and was bummed because I was using classes that already extended the SoapClient() class and didn't want to screw around with it too much.

However if you add the "exceptions"=>0 tag when you initiate the class, it won't throw a Fatal Error (though it will print an exception):

SoapClient($soapURL, array("trace" => 1, "exceptions" => 0));

Doing that allowed me to run __getLastRequest() and analyze the XML I was sending.

Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
andrewembassy
  • 117
  • 1
  • 7
1

I don't believe there is a way that you'll be able to see any XML that's being created... mainly because the function is failing on it's attempt to create/pass it.

Not sure if you tried already, but if you're having trouble trying to decide what exactly you need to pass into the function you could use:

$client->__getTypes();

http://us3.php.net/manual/en/soapclient.gettypes.php

Hope this helps!

Hkachhia
  • 4,463
  • 6
  • 41
  • 76
BrandonCS
  • 57
  • 4
  • Given the exception he gets ('SoapException: Server was unable to process request'), the error is raised on the server side, so he should be able to inspect the sent XML via `__getLastRequest()`, if he uses the 'trace' option. – Henrik Opel Oct 26 '09 at 18:43