Reputation: 127
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
Upvotes: 3
Views: 3162
Reputation: 127
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.
Upvotes: 1