REST Webservice: PUT & Delete methods

Hi All

I am using Guzzle PHP client to make REST calls for PUT and DELETE.

On the server I have this code in my index.php file where the request is being made.


//index.php
echo $_SERVER['REQUEST_METHOD'], '<BR>';
print_r($_REQUEST);
print_r(getallheaders())

For GET and POST methods, I am getting the values of all three variable/functions ie. _SERVER['REQUEST_METHOD'], _REQUEST and getallheaders(), but in case of PUT and DELETE methods I am only getting value of _SERVER['REQUEST_METHOD'] i.e PUT or DELETE and just the data in the getallheaders(), but nothing in the _REQUEST variable.

What could be the reason?

Any inputs will be great…

Thanks

Hi,

The in PHP, the data for incoming PUT and DELETE requests is available via the php://input stream. You can get the contents of the stream using file_get_contents("php://input") but the data is returned as a query string, so you’ll probably want to parse that out into separate variables:

parse_str(file_get_contents("php://input"),$post_vars);

This will give you an array, $post_vars, with the request data.

Hi

Thanks for your reply.

Yes, using your method I can now see some output at least…I have two queries:

Following is the code I am using to make a REST request…The $data variable contains the data in array() format…

For example:

$data = array(
‘a’ => 1,
‘b’ => 2
);


$ch = curl_init();
               switch($methodName){
                    case 'PUT':
                        curl_setopt($ch, CURLOPT_URL, $apiUrl);
                        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
                        curl_setopt($ch, CURLOPT_POST, true);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT') );
                        break;
                    case 'DELETE':
                        curl_setopt($ch, CURLOPT_URL, $apiUrl);
                        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
                        curl_setopt($ch, CURLOPT_POST, true);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: DELETE') );
                        break;

                }
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.

                $data = curl_exec($ch);
                curl_close($ch);

Question 1) For both PUT and DELETE, When I use:

echo file_get_contents("php://input");

I get the output:


------------------------------42c0c61e0e16
Content-Disposition: form-data; name="1"

Array
------------------------------42c0c61e0e16
Content-Disposition: form-data; name="2"

Array
------------------------------42c0c61e0e16--

and when I use:


parse_str(file_get_contents("php://input"),$post_vars);

print_r($post_vars);

I get this output:


Array
(
    [------------------------------d2bcc8532b06
Content-Disposition:_form-data;_name] => "1"

Array
------------------------------d2bcc8532b06
Content-Disposition: form-data; name="2"

Array
------------------------------d2bcc8532b06--

)


There is not much difference as I can see. Why?

Question 2) In the above output, I can see just the values i.e. 1 and 2 and not the key i.e. ‘a’ and ‘b’…why are they not included in the output?

Thanks

Try replacing this:


curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT') );

with this:


curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

works like a charm, thanks

Hi again

What about getting data from HEAD method? How do we do that?

Both file_get_contents(“php://input”) and $_REQUEST does not seem to output anything :frowning:

Thanks

Hi,

HEAD requests are indentical GET requests except that they don’t return any response body. I’ve run a simple test on my machine and query string variables are available to PHP in the $_GET array as normal. How are you making the request?