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

Passing CDATA with SoapClient?

P: 7
Hey guys, I've posted a question on here before but I don't think I was very direct about the problem so I'll state it in a new way:

How do I send CDATA to a webservice using the PHP SoapClient?

Currently I am creating a $string full of CDATA and XML tags. But when I pass this to the webservice, the CDATA and XML tags are escaped and lose their meaning.

Expand|Select|Wrap|Line Numbers
  1. $client = new SoapClient("my.wsdl",array("trace"=>1));
  2. $string = "<! [CDATA[<myxml>]]>";
  3. $client->remoteMethod(array("tagname" => $string));
  4.  
When done this way, $string's tags are converted to &lt; and &gt; but "<tagname>" is proper -- how do I properly pass CDATA to a webservice using the PHP SoapClient?
Jun 13 '07 #1
Share this Question
Share on Google+
11 Replies


Motoma
Expert 2.5K+
P: 3,235
Hey guys, I've posted a question on here before but I don't think I was very direct about the problem so I'll state it in a new way:

How do I send CDATA to a webservice using the PHP SoapClient?

Currently I am creating a $string full of CDATA and XML tags. But when I pass this to the webservice, the CDATA and XML tags are escaped and lose their meaning.

Expand|Select|Wrap|Line Numbers
  1. $client = new SoapClient("my.wsdl",array("trace"=>1));
  2. $string = "<! [CDATA[<myxml>]]>";
  3. $client->remoteMethod(array("tagname" => $string));
  4.  
When done this way, $string's tags are converted to &lt; and &gt; but "<tagname>" is proper -- how do I properly pass CDATA to a webservice using the PHP SoapClient?
I think your problem is this:
CDATA tags must start <![CDATA[ and end ]]>
You have a space between the ! and the [
This is incorrect.
Jun 13 '07 #2

P: 7
In my code, I use <![CDATA[start_to_end]]> but since looking at many web examples I often see the tag labeled < ![CDATA[start_to_end]]>. In any case, PHP behaves the same way and escapes the <> tags around CDATA and any <> tags inside of [start_to_end].

How do I properly pass CDATA to a webservice using PHP SoapClient?
Jun 13 '07 #3

Motoma
Expert 2.5K+
P: 3,235
In my code, I use <![CDATA[start_to_end]]> but since looking at many web examples I often see the tag labeled < ![CDATA[start_to_end]]>. In any case, PHP behaves the same way and escapes the <> tags around CDATA and any <> tags inside of [start_to_end].

How do I properly pass CDATA to a webservice using PHP SoapClient?
I am not familiar with SoapClient, however from the documentation I have been reading, it appears that the SoapClient is a layer above the type of interactions you are trying to deal with. If you want to be able to send you own formed XML, you will need to utilize curl, otherwise you SoapClient is going to (properly) escape your elements as if there were data segments of its own XML structure.
Jun 13 '07 #4

P: 7
I am not familiar with SoapClient, however from the documentation I have been reading, it appears that the SoapClient is a layer above the type of interactions you are trying to deal with. If you want to be able to send you own formed XML, you will need to utilize curl, otherwise you SoapClient is going to (properly) escape your elements as if there were data segments of its own XML structure.
Motoma thanks for your continued help. Can you tell me what documentation you're looking at? I'm ready to buy a book if there's a good one you can recommend.

I've tried generating a few different objects that are compatible with the native SoapClient param objects but haven't found any. There has got to be a way to specify "This is CDATA" to SoapClient but I haven't figured it out yet.

I also wonder if I change the WSDL parameter from "s:string" to something else ("any" perhaps?) if SoapClient will quit escaping my data.

My current solution has been to use an ssl socket connection and do the HTTP headers myself. It took a lot of effort to get this done and I don't want to repeat it for future webservice calls, that's how I got here.
Jun 13 '07 #5

Motoma
Expert 2.5K+
P: 3,235
Motoma thanks for your continued help. Can you tell me what documentation you're looking at? I'm ready to buy a book if there's a good one you can recommend.

I've tried generating a few different objects that are compatible with the native SoapClient param objects but haven't found any. There has got to be a way to specify "This is CDATA" to SoapClient but I haven't figured it out yet.

I also wonder if I change the WSDL parameter from "s:string" to something else ("any" perhaps?) if SoapClient will quit escaping my data.

My current solution has been to use an ssl socket connection and do the HTTP headers myself. It took a lot of effort to get this done and I don't want to repeat it for future webservice calls, that's how I got here.
Just a brief scrub through http://php.net/soap.
It can't hurt to try manipulating the WSDL, post back if you find a solution.
Jun 13 '07 #6

P: 7
Just a brief scrub through http://php.net/soap.
It can't hurt to try manipulating the WSDL, post back if you find a solution.
Motoma, I have read, re-read and triple checked everything about SoapClient on PHP.net - what in there makes you think it's normal for SoapClient to escape your data?
Jun 13 '07 #7

Motoma
Expert 2.5K+
P: 3,235
Motoma, I have read, re-read and triple checked everything about SoapClient on PHP.net - what in there makes you think it's normal for SoapClient to escape your data?
A) This is the way it is working.
B) SOAP is an XML envelope to wrap around client data. It is an Abstraction Layer to allow you to interface with Web Services. From what I can tell, you are trying to pass XML to your remote service function. Why not send the raw data?
Jun 13 '07 #8

P: 7
A) This is the way it is working.
B) SOAP is an XML envelope to wrap around client data. It is an Abstraction Layer to allow you to interface with Web Services. From what I can tell, you are trying to pass XML to your remote service function. Why not send the raw data?
Well the remote service function _expects_ me to pass an XML doc CDATA'd up into one parent tag. I think it's a poor implementation on their part because they should just extend the CDATA'd XML to be part of the remote service parameters, but I can't change the way their service runs.

So I _am_ sending the raw data, the issue is that should I run into this again I don't want to spend hours repeating it for a different webservice call. Thanks for all your help, but I think I'm searching in vain. Maybe SoapClient will be better documented after the release of PHP6, or even less likely, I learn C and read straight from the source code LOL
Jun 13 '07 #9

Motoma
Expert 2.5K+
P: 3,235
Yes, those are both quite unlikely, aren't they :P

You are missing out on one point that I made earlier: SoapClient and NuSOAP (another PHP SOAP implementation) utilize cURL behind the scenes. You could just write your own SOAP class that wraps cURL and performs the way you need it to.
Jun 13 '07 #10

P: 2
You can still use associative array with SoapVar to pass CDATA, here is one example:

Expand|Select|Wrap|Line Numbers
  1. $cdata = new SoapVar($xmlContent, XSD_STRING);
  2. $data = [
  3.   'prop1' => 'value1',
  4.   'prop2' => 'value2',
  5.   'prop3' => $cdata,
  6. ];
  7.  
  8. $soapClient->__soapCall('method_name', $data);
  9.  
This is where I found the answer and it works for my case.
1 Week Ago #11

P: 2
Actually my last answer didn't really work because the content got encoded, what really worked was this:

Expand|Select|Wrap|Line Numbers
  1. $wholeTag = new \SoapVar("<anytag><![CDATA[{$xmlContent}]]></anytag>", XSD_ANYXML);
  2. $data = [
  3.   'prop1' => 'value1',
  4.   'prop2' => 'value2',
  5.   'prop3' => $wholeTag,
  6. ];
  7.  
  8. $soapClient->__soapCall('method_name', $data);
  9.  
When you use ANYTYPE, it won't encode anything, but also won't create a new node for some reason, so I had to hardcode the node inside SoapVar.
1 Week Ago #12

Post your reply

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