3

How to send a POST request to an external API and parse its XML response?

Normally using only php and xml I would do something like . But i am not sure how can I to do the same using Symfony.

Also please note that the content inside the xml file is dynamic like "Itemnumber", "quantity" etc

Moreinfo:

I am trying to send the xml data to our third party client system(they just work with XML so no JSON response) and parse this XML response and show it to our customers. Well since customer request are dynamic so does the contents inside the .xml file changes every-time. So i need to figure out

  1. Load this dynamic .xml file.
  2. Make the connection using POST.
  3. Read the Parse Xml data which i receive from our client(which can be done easily).
4
  • See that : stackoverflow.com/questions/561816/…. I think fit your needs. With curl you can do a post request Commented Jan 25, 2016 at 12:24
  • but the contents of xml file is dynamic so i also need to figure out the how to pass parameters to xml file Commented Jan 25, 2016 at 13:14
  • What do you mean with "dynamic"? Is your question how send and use parameters with the POST request? Commented Jan 25, 2016 at 15:15
  • ohh i meant using parameters.. i came across this one stackoverflow.com/questions/21480159/… let you know how it goes Commented Jan 25, 2016 at 15:34

1 Answer 1

1

Okay I got it working. Its simple

Step 1: Create XMLDocument as stated here OR See Example1.

public function XMLDocument($param){

// Code from the link

return $xmlDoc;
}

Step 2: Make The Connection: Use this and to get XML object

public function APiConnection($xmldoc){

 // code from the link

$reponse = curl_exec($ch);
$xmli = new SimpleXMLElement($response);
return $xmli; // returns XML Object
}

Step3: Parser the data (which is easy)

Your Action method Should look like:

public function DataAction(){

$doc = $this->XMLDocument($param);
$Data = $this->APiConnection($doc);

// parser the xml data
$price= $data->Items['0']->Price;

}

I am open for suggestions let me know if you find any mistakes.

I also came across this document from Symfony but have not used.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.