Solved

PHP and CDATA

Posted on 2013-11-21
18
1,569 Views
Last Modified: 2013-11-21
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
Comment
Question by:onepiolin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
18 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39666112
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
 

Author Comment

by:onepiolin
ID: 39666142
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
 
LVL 58

Expert Comment

by:Gary
ID: 39666168
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
Technology Partners: 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!

 

Author Comment

by:onepiolin
ID: 39666214
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
 
LVL 58

Expert Comment

by:Gary
ID: 39666232
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
 

Author Comment

by:onepiolin
ID: 39666256
I tried that. Doesn't seem to work.  Seems that CDATA is of binary type?
0
 
LVL 58

Expert Comment

by:Gary
ID: 39666364
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
 

Author Comment

by:onepiolin
ID: 39666393
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
 
LVL 58

Expert Comment

by:Gary
ID: 39666417
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
 

Author Comment

by:onepiolin
ID: 39666450
I don't see anything wrong.  The Wdsl server returns with "Fail - Invalid XML"

SoapGUI returns "Success" with the same string.
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 250 total points
ID: 39666489
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
 

Author Comment

by:onepiolin
ID: 39666518
I'd rather not post it publicly. check your email,
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39666786
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
 
LVL 58

Expert Comment

by:Gary
ID: 39666793
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
 
LVL 58

Expert Comment

by:Gary
ID: 39666802
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
 
LVL 58

Expert Comment

by:Gary
ID: 39666825
If I do a dump of the available functions I only get this

SubmitSurveyResponse
0
 

Author Comment

by:onepiolin
ID: 39666851
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
 
LVL 58

Expert Comment

by:Gary
ID: 39666877
So basically my second comment ;o)
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question