Create new item with xml


(Diroberto V) #1

Hello all,

we would like to create two applications that communicate with each other through rest api.

APP1 <–> APP2

APP1 Post an Xml to APP2, APP2 use this Xml to store informations inside DB (active record), APP2 answer to APP1 with some kind of ack.

I’m wondering, starting from Rest API of YII, “the rigth way”.

So far I have managed to create a new item only posting parameters via json format with curl:


 

 $curl = curl_init();

 curl_setopt($curl, CURLOPT_URL, url);

 curl_setopt($curl, CURLOPT_POSTFIELDS, array('code'=>'456'));//Setting post data as xml

 curl_exec($curl);

 curl_close($curl);



I’ve tried with this code but it doesn’t work and i’ve not idea if it is the correct way:




$xml = '<item1><code>Code1</code></item1>';

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, url);

        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/xml','Content-Type: application/xml'));

        curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);//Setting post data as xml

        $response=curl_exec($curl);

        curl_close($curl);



Thanks a lot


(Alirz23) #2

although it is possible post xml to the server with post with some work around, I would not recommend that simply post your data as key=value pair much like your normal form and let other service consume it like a post request when you return an item from that database then you can format the data in xml however you like.