Solved

PHP and CDATA

Posted on 2013-11-21
18
1,391 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

15 Experts available now in Live!

Get 1:1 Help Now