How can a PHP REST API receive PUTted data?

ibanez404

New Member
I'm writing an API and want to follow the REST approach. As I understand it, if I want to let API users update specific records a \[code\]PUT http://server/specific_resource\[/code\] type request should be supported. Of course, they're not doing a \[code\]GET\[/code\] and they'll need to pass along the new data and my question is how to do this (in my specific case only updating some of the fields of the specified record, but that's not so relevant here). There are two approaches I can think of: including the data in the request body (using curl: \[code\]curl -X PUT -d "key=value" http://server/specific_resource\[/code\]) or in a query string (using curl: \[code\]curl -X PUT http://server/specific_resource?key=value\[/code\]).Unfortunately, regardless of the approach I take, it seems very hard to get the provided data. The problem seems to be that PHP only really completely understands two HTTP methods, \[code\]GET\[/code\] and \[code\]POST\[/code\], with \[code\]PUT\[/code\] considered to be for file uploads. If I include the data in the body then the only way to access it seems to be via an \[code\]fopen('php://input')\[/code\] call. For instance, \[code\]http_get_request_body()\[/code\] doesn't provide the data. Likewise, the information can't be found in the \[code\]$_REQUEST\[/code\] superglobal. If I don't want to have to process the raw request body with \[code\]fopen('php://input')\[/code\] then it appears that I must send the data as query string parameters, as the data will appear as elements of the \[code\]$_GET\[/code\] superglobal (and so also \[code\]$_REQUEST\[/code\]).I'm specifically using CakePHP and it seems to only populate the \[code\]form\[/code\] array of the \[code\]params\[/code\] array in my controller method if the request was a POST. Query string parameters are put in \[code\]params\[/code\]' \[code\]url\[/code\] array regardless of the request method if used in the request URL. Not surprisingly, I'm not the only one who has run into this.What is the solution that you'd suggest? Process the input stream? Use query string parameters? Just forget the \[code\]PUT\[/code\] verb and use \[code\]POST\[/code\] instead?
 
Back
Top