PHP DOm XML Formatting

Hi,

Following the advice from a recent posting, I am using the PHP DOM to generate an XML API call to Ebay.

The XML that is generated by the DOM is indented and, due to the length of the authentication token, has forced line breaks.

If I try to get a response using the XML via simplexml_load_file, I get a failure response.

If I copy and paste the outputted XMl into the ebay API test tool, it also fails.

However if I remove all the generated white space and line breaks it works.

Any ideas for removing these automatically from the PHP DOM generated XML i.e. so that it is all collapsed in terms of formatting?

I have tried setting dom->formatOuput to both true and false with no apparent effect.

Many thanks in advance.

The formatOutput property would be my first suggestion. Can you show how you currently generate the XML, and receive Ebay’s response?

DOM doesn’t format the structure in my test code, it does however add a trailing line feed character; which could be removed using trim.

Extracts of the relevant code…



$GetCategoriesRequest_vars=array();
$GetCategoriesRequest_vars['XMLNS']="urn:ebay:apis:eBLBaseComponents";
$GetCategoriesRequest_vars['DetailLevel']="ReturnAll";
$GetCategoriesRequest_vars['ErrorLanguage']="en_GB";

...

//call the function and pass the vars
$GetCategoriesCall=CreateXMLforGetCategoriesCall($GetCategoriesRequest_vars);
$response=simplexml_load_file($GetCategoriesCall);

...

function CreateXMLforGetCategoriesCall($GetCategoriesRequest_vars) {
$dom=new DOMDocument('1.0','utf-8');
$dom->formatOutput=true;
//Create the root element and namespace
$element_root=$dom->createElement('GetCategoriesRequest');
$dom->appendChild($element_root);
//set the namespace as attribute
$element_root->setAttribute("xmlns", $GetCategoriesRequest_vars['XMLNS']);
//create the DetailLevel node and populate
$element_DetailLevel=$dom->createElement('DetailLevel', $GetCategoriesRequest_vars['DetailLevel']);
$element_root->appendChild($element_DetailLevel);
...
// tell the browser the output is XML via the 'Content-Type' HTTP header 
header('Content-Type: text/xml');
// display DOM document 
return $dom->saveXML();
}


XML Output is
<?xml version=“1.0” encoding=“utf-8” ?>

  • <GetCategoriesRequest xmlns=“urn:ebay:apis:eBLBaseComponents”>
    <DetailLevel>ReturnAll</DetailLevel>…

  • <RequesterCredentials>
    <eBayAuthToken>token</eBayAuthToken>
    </RequesterCredentials>
    <LevelLimit>3</LevelLimit>
    <ViewAllNodes>1</ViewAllNodes>
    </GetCategoriesRequest>

Each line is indented and the formatting also contains expand and contract icons.

Could calling the function name from simplexml_load_file be relevant? Should I replace with an external file?

Thanks

If you change the content-type to plain/text, hopefully you’ll see there are no line breaks and/or indenting.

I would guess your browser is performing this for you as you’re sending the text/xml content-type.

These lines will cause issues:


//call the function and pass the vars 
$GetCategoriesCall=CreateXMLforGetCategoriesCall($GetCategoriesRequest_vars); 
$response=simplexml_load_file($GetCategoriesCall); 

The function CreateXMLforGetCategoriesCall() returns an XML string, which you then pass in to simplexml_load_file(), which expects a file path/URL but is being given a string. PHP should be outputting a warning message there!

Your XML ouput looks like what the browser is displaying (in pretty colours with collapsible sections, right?). Use the “view source” feature of your browser, or output it as plain text, to see the actual output.

Whether I use text/xml or text/plain, the browser-rendered html has the indents etc.

However if I view the source with the text/xml version, it is still indented. View source with text/plain and the wrapping tags are replaced with < etc which still isn’t accepted by the Ebay web service.

Any suggestions very much appreciated. I’m very new to API stuff and don’t feel I’m making the progress I’d hoped to.

Hi Salathe,

Thanks for that-I wondered whether that might be the problem. I’ve tried separating the files: get_categories.php now has…


set vars
run function which returns the xml

echo $GetCategoriesCall;

so that viewed in a browser is the rendered xml.

make_call.php contains nothing other than the line
$response=simplexml_load_file(“get_categories.php”);

but this is generating:
Warning: simplexml_load_file() [function.simplexml-load-file]: get_categories.php:75: parser error : ParsePI: PI php never end … in /home/multe/public_html/make_call.php on line 4

Any ideas?

You’re telling SimpleXML to load the contents of that PHP file (the actual PHP code!), not any XML.

Hi Salathe,

Yes - I think it’s all becoming clear!

I’ve changed the make_call code to:

$url=file_get_contents(“get_categories.php”);
$response=simplexml_load_file($url);

I’m still getting error messages but they relate to missing variables not being passed to the function so I can see how it all fits together now and what I’ve been doing wrong.

To yourself and Anthony - many thanks - really appreciated.