简体   繁体   中英

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

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM