PHP REST options put / delete

Trying to understand the REST method for creating applications in PHP .

I had a problem understanding the put / delete dispatch from a PHP script.

On the Internet, I can learn how to determine which php method was sent.

 if($_SERVER['REQUEST_METHOD'] == 'DELETE') 

But how to send this delete method?

Ok, what do I do when I want to delete some record from DB I have a normal html form with the method set to post / get and write the db identifier, then press the submit button to send post / get .

How to create this submit to send delete / put methods?

+6
source share
4 answers

There are two common ways to send a request from an HTML page using the http method other than GET or POST.

# 1 : use the html form to submit a POST request, but specify a hidden form field that tells the server to process the request as if it were using a different method. This is the approach indicated by @xdazz .

 <form method="post" action="my_resource.php"> ... <input type="hidden" name="REQUEST_METHOD" value="PUT" /> <form> 

In your PHP script, "my_resource.php" you will need to look at both the real request method and the submit form field to determine which logic to call:

 /* my_resource.php */ $method = strtolower($_SERVER['REQUEST_METHOD']); if( $method === 'post' && isset($_REQUEST['REQUEST_METHOD'])) { $tmp = strtolower((string)$_REQUEST['REQUEST_METHOD']); if( in_array( $tmp, array( 'put', 'delete', 'head', 'options' ))) { $method = $tmp; } unset($tmp); } // now, just run the logic that appropriate for the requested method switch( $method ) { case "get": // logic for GET here break; case "put": // logic for PUT here break; case "post": // logic for POST here break; case "delete": // logic for DELETE here break; case "head": // logic for DELETE here break; case "options": // logic for DELETE here break; default: header('HTTP/1.0 501 Not Implemented'); die(); } 

Note. You can put the above logic on each page (or call it from each page). An alternative is to create a proxy script (for example, "rest-form-proxy.php" ). Then all forms on your site will be sent to the proxy server, including request_method, and the destination URL. The proxy will retrieve the provided information and redirect the request to the required URL using the appropriate requested http method.

A proxy approach is a great alternative to embedding logic in every script. If you are still creating a proxy server, be sure to check the requested URL and do not allow a URL that does not point to your own site. Failure to perform this check will allow other users to use your proxy server to launch malicious attacks on other sites; and it may also compromise the security and / or privacy of your website.

-

# 2 Use Javascript on your HTML page to trigger XMLHttpRequest . This is a more complex approach that requires a bit of javascript, but in some cases it can be more flexible. It allows you to send requests to the server without reloading the page. It also allows you to send data in many different formats (you are not limited to sending only data from an html form). For instance:

 <button onclick="doSave()">Save</button> <script> var myObject = { // ... some object properties that // that you'll eventually want to save ... }; function doSave() { var xhr = createxmlhttprequest(); // initialize the request by specifying the method // (ie: "get", "put", "post", "delete", etc.), and the // url (in this case, "my_resource.php"). The last param // should always be `true`. xhr.open("put", "my_resource.php", true); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onreadystatechange = function() { if (xhr.readystate != 4) { return; } var serverresponse = xhr.responsetext; // ... this code runs when the response comes back // from the server. you'll have to check for success // and handle the response document (if any). }; // this initiates the request, sending the contents // of `myObject` as a JSON string. xhr.send(JSON.stringify(myObject)); // The request runs in the background // The `onreadystatechange` function above // detects and handles the completed response. } </script> 

XMLHttpRequest is much larger than the main example above. If you choose this route, please study it carefully. Among other things, make sure that you handle the various error conditions correctly. There are also a number of compatibility issues between browsers, many of which can be resolved using an intermediary, such as the jQuery $ .ajax () function .

Finally, I must note that the two above methods are not mutually exclusive. It is possible to use forms for some requests and XMLHttpRequest for others if you create your own server so that it can handle any kind of request (as shown in No. 1 above).

+6
source

HTML forms only support GET and POST, so in a regular web application you need to use a hidden field to specify the request method, which is most of the frameworks.

 <form method="post" action="..."> ... <input type="hidden" name="REQUEST_METHOD" value="PUT" /> <form> 
+4
source

If you use Chrome, you can use Postman to test your REST service. It allows you to send any commands - DELETE, PUT, as well as OPTIONS, PATCH, etc.

In Firefox, you can use RESTClient .

+3
source

The usual way to do this is to use cURL

 $ch = curl_init('YOUR_URL'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); // curl_setopt($ch, CURLOPT_PUT, true); - for PUT curl_setopt($ch, CURLOPT_POSTFIELDS, 'some_data'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HEADER, 0); // DO NOT RETURN HTTP HEADERS curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN THE CONTENTS OF THE CALL $result = curl_exec($ch); 
+3
source

Source: https://habr.com/ru/post/923531/


All Articles