CodeIgniter Forums
How to POST XML via Phil Sturgeon's REST library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to POST XML via Phil Sturgeon's REST library (/showthread.php?tid=31944)



How to POST XML via Phil Sturgeon's REST library - El Forum - 07-07-2010

[eluser]Oskar Smith[/eluser]
Hello all,

I have a question about how to post XML via Phil Sturgeon's REST library. i.e.

http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/

I've successfully run a few GET requests but now want to POST some XML to a RESTful URL. Does anyone know how to do this? I was thinking along the lines of something like:

Code:
$xml = "
        <invoice>
          <contact-id>1234</contact-id>
          <dated-on>".date('Y-m-d')."T00:00:00Z</dated-on>
          <reference>123</reference>
          <currency>GBP</currency>
          <payment-terms-in-days>30</payment-terms-in-days>
          <comments></comments>
        </invoice>
    ";
                
$fh  = fopen('php://memory','w+');  
fwrite($fh, $xml);  
rewind($fh);
        
$params['file'] = $fh;
        
$data = $this->EE->rest->post('invoices', $params, 'application/xml');

But that doesn't work - the receiving URL is returning error messages that indicate that the XML data hasn't been parsed, so therefore hasn't been sent correctly.

And yes, I'm running this in EE 2.0 as a third party module, but that's irrelevant.

Any pointers on how to POST XML in this manner would be greatly appreciated, thanks.


How to POST XML via Phil Sturgeon's REST library - El Forum - 11-17-2010

[eluser]PaulDunn[/eluser]
I also would love to know how to do this


How to POST XML via Phil Sturgeon's REST library - El Forum - 11-17-2010

[eluser]smilie[/eluser]
Have you installed RESTFul api in your CI?

I do not use XML, but I do belive it is pretty much same as sending Json. I do it as follows:

Code:
# In controller, I load (installed) library;
# I have also added REST_SERVER (which is address of receiving controller) in the constants.php; I use only 1 controller which sorts out all calls
$this->load->library('rest', array('server' => REST_SERVER));

# Now, REST is loaded, I prepare data which I want to send (in your case, that would be XML):
$data['some']['data'] = 'some data here...';

# And then I call RESTFul API:
$saveOrder = $this->rest->post('saveOrder',$data,'json');

# Whereby:
# 'saveOrder' is the POST function name in the controller
# $data is the data itself
# 'json' is type of data I am sending

So, provide more info where are you stuck at Smile

Cheers,
Smilie