By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,589 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,589 IT Pros & Developers. It's quick & easy.

SoapClient Escaping XML Tags

P: 7
Hey guys,
I recently lost hours to the PHP 5.2.1 SoapClient when trying to use a remote webservice. There are two methods I have to use, the first one worked like a charm and the second one has me fuming mad. The two methods are "GetSessionToken" which expects two strings, and "SetProjectData" which expects XML wrapped in a CDATA tag.

My problem is that the XML I pass to SetProjectData is being escaped by the PHP SoapClient and I don't know how to turn this off. The XML characters < > and & are being escaped to &lt; &gt; and &amp; -- I need this to stop! It's not documented on php.net anywhere and I don't know where to turn.

Sample:
Expand|Select|Wrap|Line Numbers
  1. $client = new SoapClient('my.wsdl',array('tracing'=>1));
  2.  
  3. $getSessionTokenParams = array('username'=>$username,'password'=>$password); //When passed to the webservice in the next line, this creates two XML tags, <username> and <password> with the appropriate values inside
  4.  
  5. $client->GetSessionToken($getSessionTokenParams); //Runs properly, gives me a session token.
  6.  
  7. $setProjectDataPayload = "<![CDATA[<data><projectinfo>details</projectinfo></data>]]>"; //Webservice expects inside XML to be wrapped in CDATA
  8.  
  9. $setProjectDataParam = array("sdsXML" => $setProjectDataPayload); //Webservice expects CDATA to be inside one sdsXML tag
  10.  
  11. //Here's where it breaks:
  12. $client->SetProjectData($setProjectDataParam); //Remote webservice fails with "not properly formated XML" error
  13. echo $client->__getLastRequest(); //Output the Webservice request and what do I find?  $setProjectDataPayload has all of the XML tags <, > and & have all been escaped and I didn't do it.  The sdsXML tag itself is not escaped, only its contents.  Why is this happening?  Where is this documented?
  14.  
[Please use CODE tags when posting source code. Thanks! --pbmods]

Most importantly: How the heck do I turn "feature" off? I just want it to pass the literal data I send to it, please no chracter escaping!
Jun 8 '07 #1
Share this Question
Share on Google+
4 Replies


P: 18
Hey guys,
I recently lost hours to the PHP 5.2.1 SoapClient when trying to use a remote webservice. There are two methods I have to use, the first one worked like a charm and the second one has me fuming mad. The two methods are "GetSessionToken" which expects two strings, and "SetProjectData" which expects XML wrapped in a CDATA tag.

My problem is that the XML I pass to SetProjectData is being escaped by the PHP SoapClient and I don't know how to turn this off. The XML characters < > and & are being escaped to &lt; &gt; and &amp; -- I need this to stop! It's not documented on php.net anywhere and I don't know where to turn.

Sample:
Expand|Select|Wrap|Line Numbers
  1. $client = new SoapClient('my.wsdl',array('tracing'=>1));
  2.  
  3. $getSessionTokenParams = array('username'=>$username,'password'=>$password); //When passed to the webservice in the next line, this creates two XML tags, <username> and <password> with the appropriate values inside
  4.  
  5. $client->GetSessionToken($getSessionTokenParams); //Runs properly, gives me a session token.
  6.  
  7. $setProjectDataPayload = "<![CDATA[<data><projectinfo>details</projectinfo></data>]]>"; //Webservice expects inside XML to be wrapped in CDATA
  8.  
  9. $setProjectDataParam = array("sdsXML" => $setProjectDataPayload); //Webservice expects CDATA to be inside one sdsXML tag
  10.  
  11. //Here's where it breaks:
  12. $client->SetProjectData($setProjectDataParam); //Remote webservice fails with "not properly formated XML" error
  13. echo $client->__getLastRequest(); //Output the Webservice request and what do I find?  $setProjectDataPayload has all of the XML tags <, > and & have all been escaped and I didn't do it.  The sdsXML tag itself is not escaped, only its contents.  Why is this happening?  Where is this documented?
  14.  
[Please use CODE tags when posting source code. Thanks! --pbmods]

Most importantly: How the heck do I turn "feature" off? I just want it to pass the literal data I send to it, please no chracter escaping!
I haven't used PHP's built in SOAP client so I can't help you with how to turn the escaping off.. however, if you remove the <![CDATA[ tags it would be equivalent to the actual CDATA wrapped in <![CDATA[.
It would work unless the soap server actually needs the <![CDATA[ tags?
Jun 9 '07 #2

P: 7
The server expects the CDATA tags for one, and more importantly the XML I send inside of CDATA is being escaped anyways: Even if I remove the CDATA text, I still end up with &lt;myxmltag&gt; and the remote call fails.

I can't be the first person to use CDATA with PHP's SoapClient. I just can't figure out why in the world my data is being escaped.

Edit** In my original code, I pass:
Expand|Select|Wrap|Line Numbers
  1. array('tracing'=>1)
in the SoapClient constructor but the proper value is actually
Expand|Select|Wrap|Line Numbers
  1. array('trace'=>1)
Jun 10 '07 #3

P: 1
I just wrote a simple class to do it.

Signed up to help you out, so, I hope it does!

Expand|Select|Wrap|Line Numbers
  1. class XMLClient extends SoapClient {
  2.  
  3.     public function XMLClient($wsdl) {
  4.         try {
  5.           parent::__construct($wsdl);
  6.         } catch (SoapFault $exception) {
  7.             $this->error = $exception;
  8.         }
  9.     }
  10.  
  11.     public function __doRequest($request, $location, $action, $version) {
  12.         $request = str_replace("&lt;", "<", $request);
  13.         $request = str_replace("&gt;", ">", $request);
  14.         print "Request - " . $request;
  15.         return parent::__doRequest($request, $location, $action, $version );
  16.     }
  17. }
  18.  
Usage:

Expand|Select|Wrap|Line Numbers
  1. $x = XMLClient("my.wsdl");
  2. $x->getSomeBooks("<some><xml><content></content></xml></some>");
  3.  
Oct 30 '07 #4

P: 1
I had the same problem. After some trail and error. If found a solution. You could use SoapVar with XSD_ANYXML.

Expand|Select|Wrap|Line Numbers
  1. new SoapVar("<param2><![CDATA[<html><body>Some content</body></html>]]> </param2>", XSD_ANYXML)
Blogged it over here: http://bit.ly/e8oPrt
Mar 24 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.