Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1904
  • Last Modified:

PHP and CDATA

I'm a bit lost here, I'm barely scratching the surface on php and I hit a roadblock.

I am attempting to encapsulate in CDATA an XML string so that the end result (String) shows exactly this:

<![CDATA[<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<first>john</first>
<last>doe</last>
<phone>15125552154</phone>
</root>]]>

 
I tried using the DOMDocument method and it gives me an error:

$xmlString = '<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<first>john</first>
<last>doe</last>
<phone>15125552154</phone>
</root>';
	
$dom=new DOMDocument('1.0','iso-8859-1');
$dom->appendChild($dom->createElement());
$cdataNode=$dom->createCDATASection($xmlString);

Open in new window


Eventually, this string needs to be sent via soap to a wsdl server. I got the soap call to the wsdl server to work fine but now I can't seem to be able to build the string to send to the server.

My issue is that all I want to do is encapsulate it. I don't want to build an xml structure, so I can't really use the appendChild() / createElement() property and I have no idea how to encapsulate the string in CDATA.

Any help is greatly appreciated.
0
onepiolin
Asked:
onepiolin
  • 10
  • 7
2 Solutions
 
GaryCommented:
Confused by what you are doing/want.  If you put the whole xml in CDATA then the whole xml would be ignored.
Normally you use CDATA to hide data in an xml string from the xml parser.
0
 
onepiolinAuthor Commented:
Sorry. I am confused myself. What it boils down to is that I need to pass a CDATA encoded string to a wsdl server.

I can't seem to be able to encode the data. This is my futile attempt to use a string for submission:

<?php

$xmlString = '<![CDATA[<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<first>john</first>
<last>doe</last>
<phone>15125552154</phone>
</root>';;


$wsdl ="...URL...?wsdl";

$client = new SoapClient($wsdl);

$parameters = array("strSubmission"=>$xmlString);

$value = $client->SubmitData($parameters);

$xml_response = $value->SubmitDataResult;

print "<pre>\n";

print_r($xml_response);

print "</pre>";


?>

Open in new window


I basically need a way to encode $xmlString to CDATA.
0
 
GaryCommented:
Why are your trying to wrap it in CDATA - this is confusing me?
Why are not just sending the XML as it is.
If you send your XML wrapped in CDATA to the receiving wsdl server it would (or should) ignore everything inside the CDATA tags.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
onepiolinAuthor Commented:
That is the requirement from the wsdl server host. I tested it using SoapGUI and it worked fine. If I just send the XML string it fails.

In a nutshell, the host is taking the XML data and parsing into a database. I don't know enough about Wsdl to know why they did it that way.

...thus my task to encode a string in CDATA.
0
 
GaryCommented:
Then just wrap your string in CDATA tags - I must be missing something obvious here.

$xmlString = '<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<first>john</first>
<last>doe</last>
<phone>15125552154</phone>
</root>';

$xmlString = '<![CDATA['.$xmlString.']]>';
0
 
onepiolinAuthor Commented:
I tried that. Doesn't seem to work.  Seems that CDATA is of binary type?
0
 
GaryCommented:
CDATA is just a tag - it doesn't do anything.
Can you post your real code - you seem to have posted 2 versions of what you have.
0
 
onepiolinAuthor Commented:
The latest is what I need. With the previous one I was looking to see if I could use that to encode the string, but it only encodes strings within XML tags.

<?php

$xmlString = '<![CDATA[<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<first>john</first>
<last>doe</last>
<phone>15125552154</phone>
</root>';;


$wsdl ="...URL...?wsdl";

$client = new SoapClient($wsdl);

$parameters = array("strSubmission"=>$xmlString);

$value = $client->SubmitData($parameters);

$xml_response = $value->SubmitDataResult;

print "<pre>\n";

print_r($xml_response);

print "</pre>";


?>

Open in new window

0
 
GaryCommented:
What is wrong with
$xmlString = '<![CDATA[<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<first>john</first>
<last>doe</last>
<phone>15125552154</phone>
</root>]]>'; 

Open in new window

0
 
onepiolinAuthor Commented:
I don't see anything wrong.  The Wdsl server returns with "Fail - Invalid XML"

SoapGUI returns "Success" with the same string.
0
 
GaryCommented:
That's because it is invalid XML when your wrap it up.
I find it strange the server is expecting an xml string wrapped in CDATA.
Can you name the company you are posting to?
0
 
onepiolinAuthor Commented:
I'd rather not post it publicly. check your email,
0
 
Ray PaseurCommented:
This is why knowledgeable developers write RESTful APIs instead of SOAP APIs!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12239-Introduction-to-Application-Programming-Interfaces.html

Maybe they want something like this:
http://www.laprbass.com/RAY_temp_onepiolin.php

<?php // RAY_temp_onepiolin.php
error_reporting(E_ALL);

// THE CDATA WE WANT TO EMBED CONTAINS XML
$innerData = <<<END
<![CDATA[<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<first>john</first>
<last>doe</last>
<phone>15125552154</phone>
</root>]]>
END;

// EMBED THE XML STRING IN THE XML DOCUMENT
$xml = <<<EOD
<?xml version="1.0" encoding="ISO-8859-1"?>
<wrapper>
  <xmlcdata>$innerData</xmlcdata>
</wrapper>
EOD;

// IS THIS VALID XML?
$obj = SimpleXML_Load_String($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
if (!$obj) trigger_error('XML FAIL', E_USER_ERROR);
echo nl2br(htmlentities($obj->xmlcdata));

Open in new window

HTH, ~Ray
0
 
GaryCommented:
This is the code I came up with
And this is the error
Error!Function ("SubmitData") is not a valid method for this serviceLast

<?php
$xmlString = '<![CDATA[<?xml version="1.0" encoding="ISO-8859-1"?><root><first>john</first><last>doe</last><phone>15125552154</phone></root>]]>';


$wsdl = 'url';
$parameters = array("strSubmission"=>$xmlString);

try
{
   $client = new SoapClient($wsdl);
   $value = $client->SubmitData($parameters);

   $xml_response = $value->SubmitDataResult;
}

catch (Exception $e)
{
   echo "Error!";
   echo $e -> getMessage ();
   echo 'Last response: '. $client->__getLastResponse();
}

Open in new window

0
 
GaryCommented:
Is it ok if I post the url in an hidden comment - Ray will be able to see it but it won't be visible publicly. At least he will know where you are posting it and he is more adept at this kinda thing than me.
0
 
GaryCommented:
If I do a dump of the available functions I only get this

SubmitSurveyResponse
0
 
onepiolinAuthor Commented:
Well, it's usually the simplest answer the correct one.

I tried the string without the CDATA tags and it took it.  You do the same in SoapUI and it bonks.

Go figure.
0
 
GaryCommented:
So basically my second comment ;o)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now