Return XML file in PHP API

Although there is a fair amount of data, I written a fairly basic API which I need to return in XML format. What I need to know is how to send this data back in an XML file. The code below displays a well-formed XML to the browser.
After preliminary processing, a string is sent to the XML processing file shown below.
Elements are delimited with |, while Key/values are delimited with ^.
   $dom = new DOMDocument("1.0", 'utf-8');
   header ("Content-Type:text/xml");
   $QArray = explode('|',$_REQUEST[Q]); // separate elements
   foreach($QArray as $value) // create key/value pairs
      $x = explode('^',$value);
      $XMLItems[$x[0]] = $x[1];

   $root = $dom->createElement("RequestResult");
   foreach($XMLItems as $key => $value)
      $key = $dom->createElement($key);
      $variable = $dom->createTextNode($value);
   echo $dom->saveXML();

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Do you need to initiate a file download or just send the data back?

How is the data going to be used - most API services expect the XML to be in the response - not a file.

Please can you clarify the requirements.
RationalRabbitAuthor Commented:
I'm a little perplexed. And this is kind of unfamiliar territory. I thought this was all they needed. One client said they needed to pull the data from the DOM. I'm fairly familiar with the HTML DOM, but not XML. They then said they needed a file. I don't know if they have any better understanding than I do, but, as I searched around the web, I found that most examples of parsing XML data start with a file. What I have here puts out a well-formatted XML string. Is this usually all that is required?
Julian HansenCommented:
How will this API be consumed - that will probably tell us what we need to know.

Creating a file is easy - use file_put_contents()
$filename = "NameOfFile.xml";
$contents = "<xmldoc>This is just for the example</xmldox>";
file_put_contents($filename, $contents);

Open in new window

The question is though - how are you going to get this file to them.

You can send them a link to it
You can initiate a download of the file by setting the document header values to indicate a file download, filename, file type and size.

But we can't really help you until we know how this data is going to be used.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Answer Julian's question for best assistance.

Do you need to initiate a file download or just send the data back?

If you're trying to output this data to a file, then use Julian's 2nd comment suggestion or just send the output of your script to a file, as in...

your-script > output.xml

Open in new window

RationalRabbitAuthor Commented:
Yes, the problem is - how do I get the file to them. And I know CURL has some functions, etc., but the request is automated on both ends. Certainly I can have them give me a file name in the API and it would be like an automated download, but I have no idea how to automate that and the big question is  - is this a common need, or is it that all they have for reference in receiving XML data indicates downloading a file. That is why I asked the question "What I have here puts out a well-formatted XML string. Is this usually all that is required [in normal industry practice]? " 
What makes this more difficult is that I do not have direct communication with these people - they are my client's client. And I am embarrassingly ignorant here. I believe I can see their perspective, if, indeed, they are as naive as I am - usually, when I have had to deal with XML data supplied from an outside source, I simply download the formatted XML file.

So, as I see it, I have two choices here:

1. I can somehow send them a perfectly formatted XML file.

or, if this is not a common API practice, and what is common is that they simply receive the data string (eg: <data><firstelement></firstelement><secondelement></secondelement></data>) and the problem is they don't know any more than I do,

2. Put together something for them in javascript or PHP, or whatever their preference is, to allow them to receive and process the data as it is.  Their complaint here is that the data does not enter the DOM and, therefore, they can't access it. And once again, my ignorance is showing. I have lots of tools to read the HTML DOM, but not XML so I couldn't say one way or another how they would address the XML DOM.
Julian HansenCommented:
Is this usually all that is required [in normal industry practice]? " 
Yes but lets examine this a bit more.

What is the difference between an XML string and a file.
Answer: nothing - except in how you setup the headers to the tell the receiving party (think browser) how you want it to handle the data.

If a string, it displays it in the browser - if a file, it throws up the file save dialog box - data is the same - it is just how it is interpreted. So when you talk about sending a string or a file you are actually defining how it will be used on the other side - not how you generate the data - which is why we  keep asking you about how it will be used.

If you have your result in an xml string and they want it as a string
echo $xml;

Open in new window

If they need it as a file
$filename = "yourfilenamehere.xml";
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="' . $filename . '"');
// More headers here for size, cache etc as needed
// ... and wait for it
echo $xml;

Open in new window

Can you see that in both the string and file scenarios above the data is still sent in exactly the same way
(echo $xml);

The only difference is how they want to consume the data.

Now onto what is standard practice.
Web services are the most common way of interacting with API's and with a web service you make a REST / SOAP / Other call to the service on a URL (optionally with parameters) and a request method (GET, PUT, POST etc) and the web service returns you a string of data (XML, JSON, HTML etc)
The receiving entity retrieves the string and does something with it.

It really does not make sense for the requesting code to want the sending code to put all the headers in to say it is a file - the data is the same regardless so why add all that extra gumpf when it is not going to be used.

heir complaint here is that the data does not enter the DOM and, therefore, they can't access it
This says that whoever is asking for this does not really understand what is going on. To say that they can't access XML data because it has not entered the DOM is confusing and incorrect on many levels.

Firstly, why do you want XML to enter the DOM? If the XML is in fact HTML then I can understand but HTML does not strictly conform to XML rules so there is an issue of semantics.

Secondly, it is the responsibility of the requesting code to do what is necessary with the returned data - if that means inserting the returned data into the DOM in some way then the calling code must handle that - there isn't a solution in existence where code from a remote service is consumed and merged with the DOM that does not pass through some intervening routine (typically AJAX).


If you can answer that you are most of the way home.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RationalRabbitAuthor Commented:
Thank you for your thorough answer, Julian. That is just what I was looking for. When your methods are being questioned by someone and you have no idea what their level of expertise is and you're pretty dumb in that area yourself, It's hard to tell heads from tails. With your response, I am actually all the way home. What they want to do with the data is their responsibility. Others seem to be using this API without any problem.
Julian HansenCommented:
@RationalRabbit - did you mean to select your own answer as the solution?
RationalRabbitAuthor Commented:
The "Close Request pending" box says 0 points. Did I do something wrong? Does that mean the accepted answer did not get credit?
RationalRabbitAuthor Commented:
No. How do I correct that?
RationalRabbitAuthor Commented:
This is the information I needed. (Believe I fixed my error in accepted answer)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.