tags:

views:

111

answers:

2

Hi Guys.

I'm using PHP5 and it's inbuilt SOAP functionality. I'm catching the SOAP Fault errors, and email myself when one is triggered.

What I really need to do is include the __getLastRequest() and __getLastResponse(), however as these are XML objects, when I try to include them by echoing them into the body of my HTML email, for obvious reasons they don't appear in full.

Is there a function or class I can use to convert these objects into a HTML friendly string?

I've googled this but without any joy. If possible i'd like to avoid using a class external to PHPs own functionality but if needs must i'll have to.

EDITED:

How can I format the following XML so that it gives me an indented browser friendly version?

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="schemas.xmlsoap.org/soap/envelope/"; xmlns:ns1="xxx.com/"><SOAP-ENV:Body><ns1:Ad… 0NE</ns1:Postcode><ns1:BuildingName></ns1:BuildingName><ns1:BuildingNo></ns1:BuildingNo><ns1:SubBuilding></ns1:SubBuilding><ns1:Organisation></ns1:Organisation><ns1:Street></ns1:Street><ns1:SubStreet></ns1:SubStreet><ns1:Town></ns1:Town><ns1:District></ns1:District></ns1:address><ns1:AccountName>[email protected]</ns1:AccountName><ns1:Password>password</ns1:Password></ns1:AddressLookupUK></SOAP-ENV:Body></SOAP-ENV:Envelope>
A: 

EDIT:

Basic print out of the xml. You may need to adjust to fit the actual xml it was hard to read since it was posted unformatted. Youll also protentialy need to handle nested elements i suppose which would require you to loop through the children. and i suppose attributes as well if you have any... You can refer to the SimpleXML docs for more detail on this.

foreach($xml->Body as $element => $value)
{
  echo "$element: $children";
}

Umm use htmlspecialchars or htmlentities to encode the string...? that way it will display as "code" in ana html email.

prodigitalson
I know of this, but again, the subject is an object not simply a string
Sjwdavies
Whats a `var_dump` of the object look like?
prodigitalson
Apologies.htmlspecialchars has worked and it now appears as the XML string.Is there a function that can let me spit this out with all it's indents intact?
Sjwdavies
Just echo it inside a `<pre>`. That should work assuming the mail client supports the tag... i think most do.
prodigitalson
I've got <pre> wrapped round it anyway, but it must be being ignored for some reason.
Sjwdavies
hmmm... yeah im not sure then. youre not running the `<pre>` through the escaping functions too right? Another option might be to send it as an attachment of some sort... i know in my client if there is an html attachment it generally displays it in the body of the email minus any non local images...
prodigitalson
Thanks for your help but i'm going to give up on this.I posted the string exactly as it is outputted on my screen.I tried what you suggested, and the following returned an EMPTY strig:$xml = new SimpleXmlElement($client->__getLastRequest());foreach($xml->Body as $element => $value){ $test .= "$element: $children";}$_SESSION['test'] = $test;
Sjwdavies
A: 

You could use http://www.php.net/manual/en/book.tidy.php eg:

<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>...</SOAP-ENV:Envelope>';
$config = array(
            'indent'         => true,
            'output-xml'     => true,
            'input-xml'     => true,
            'wrap'         => '1000');

// Tidy
$tidy = new tidy();
$tidy->parseString($xml, $config, 'utf8');
$tidy->cleanRepair();
echo tidy_get_output($tidy);
?>

but you have to install the php extension first.

Timothy