We help IT Professionals succeed at work.
Get Started

Reading returned XML data with PHP

RationalRabbit
on
383 Views
Last Modified: 2017-11-23
I have an API consisting of two files that takes a url from a user, processed it, and sends an array to another file to return XML data back to the user. My problem is that I do not know how to get the return data (if I were the user) and parse it. Using a browser, the XML will display fine. I am hoping there are PHP functions that will send the url and retrieve the result for parsing.
Here is an example of the url sent by the user:
http://www.domain.com/xmlapi.php?a=1&b=2&c=3

Open in new window

Those query vars are then processed, sent to another server, and an array of results is returned. I then create a query string from those results place that string into another query and send that to another file that returns the XML.
The query string looks like this:
XMLProcess.php?Q=Element1^Value|Element2^Value|ElementThree^Value ... etc.

Open in new window

That file looks like this
   $dom = new DOMDocument("1.0", 'utf-8');
   header ("Content-Type:text/xml");
   $QArray = explode('|',$_REQUEST[Q]);
   foreach($QArray as $value)
   {
      $x = explode('^',$value);
      $XMLItems[$x[0]] = $x[1];
   }
   $root = $dom->createElement("Requests");
   $dom->appendChild($root);
   foreach($XMLItems as $key => $value)
   {
         $key = $dom->createElement($key);
         $root->appendChild($key);
         $variable = $dom->createTextNode($value);
         $key->appendChild($variable);
      }
   }
   echo $dom->saveXML();

Open in new window

As I say, this will display well-formatted xml in a browser, but is there a way to receive and parse this xml data with PHP?
Am I creating extra problems by having a secondary processing file (XMLProcess.php), rather than just including this XML creation code in the xmlapi.php file?

I'm trying to avoid using something like CURL or SOAP, if possible.
Comment
Watch Question
This problem has been solved!
Unlock 3 Answers and 58 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE