Solved

PHP and CDATA

Posted on 2013-11-21
18
1,433 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
  • 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP 7 with IIs and SQL Server Windows 2008 Server failed to connect 8 55
phpmyadmin 3 33
How do I put a session for a radio button? 4 14
Problem sending file attachments 8 23
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now