I've set up a REST service and client in PHP and I'm having a bit of trouble with PUT.
Here's my situation: I'm coding a REST resource that should accept an array of data and an image. The REST resource should update an existing record, so I'm using PUT. I'm sending the data with a PHP curl client I wrote. So - pretty much the same situation as if you were sending a HTML multipart form to a PHP script that does a file upload and accepts some additional POST fields - except with PUT and PHP curl..
Up 'till now I've been sending the PUT request something like this (pseudo code):
$a_some_data = array('name' => 'test', 'user_id' => 4); $body = http_build_query($a_data); $fh = fopen('php://memory', 'rw'); fwrite($body); rewind($fh); $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => 'http://myapi/resource/someid', CURLOPT_PUT => TRUE, CURLOPT_INFILE => $fh, CURLOPT_INFILESIZE => strlen($body) )); curl_exec($ch);
and reading the data on the server like so:
..which works just fine.
So now I would like to add a (binary) file into the mix. - How would I implement this on the client side? - How would I deal with the file on the server?
For a test I set up a HTML form with a file input, copied the raw multipart/form-data request it sends, and tried sending that data as a file with curl in a PUT request. That kind of works, but I would have to parse the raw data on the server manually, which I'm not sure is the best idea. Alternatively, I guess I could send the file as the body of the PUT request, and add the other parameters in the URL as a query string - but I guess that kind of defies the point of a PUT REST resource..
Please share your thoughts on this. Thanks!