How can I detect which request type was used (GET, POST, PUT or DELETE) in PHP?
$request = new \Zend\Http\PhpEnvironment\Request();
$httpMethod = $request->getMethod();
In this way you can also achieve in zend framework 2 also. Thanks.
When a method was requested, it will have an array
. So simply check with count()
.
$m=['GET'=>$_GET,'POST'=>$_POST];
foreach($m as$k=>$v){
echo count($v)?
$k.' was requested.':null;
}
3v4l.org/U51TE
Detecting the HTTP method or so called REQUEST METHOD
can be done using the following code snippet.
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'POST'){
// Method is POST
} elseif ($method == 'GET'){
// Method is GET
} elseif ($method == 'PUT'){
// Method is PUT
} elseif ($method == 'DELETE'){
// Method is DELETE
} else {
// Method unknown
}
You could also do it using a switch
if you prefer this over the if-else
statement.
If a method other than GET
or POST
is required in an HTML form, this is often solved using a hidden field in the form.
<!-- DELETE method -->
<form action='' method='POST'>
<input type="hidden" name'_METHOD' value="DELETE">
</form>
<!-- PUT method -->
<form action='' method='POST'>
<input type="hidden" name'_METHOD' value="PUT">
</form>
For more information regarding HTTP methods I would like to refer to the following StackOverflow question:
HTTP protocol's PUT and DELETE and their usage in PHP
You can get any query string data i.e www.example.com?id=2&name=r
You must get data using $_GET['id']
or $_REQUEST['id']
.
Post data means like form <form action='' method='POST'>
you must use $_POST
or $_REQUEST
.
In core php you can do like this :
<?php
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
//Here Handle GET Request
echo 'You are using '.$method.' Method';
break;
case 'POST':
//Here Handle POST Request
echo 'You are using '.$method.' Method';
break;
case 'PUT':
//Here Handle PUT Request
echo 'You are using '.$method.' Method';
break;
case 'PATCH':
//Here Handle PATCH Request
echo 'You are using '.$method.' Method';
break;
case 'DELETE':
//Here Handle DELETE Request
echo 'You are using '.$method.' Method';
break;
case 'COPY':
//Here Handle COPY Request
echo 'You are using '.$method.' Method';
break;
case 'OPTIONS':
//Here Handle OPTIONS Request
echo 'You are using '.$method.' Method';
break;
case 'LINK':
//Here Handle LINK Request
echo 'You are using '.$method.' Method';
break;
case 'UNLINK':
//Here Handle UNLINK Request
echo 'You are using '.$method.' Method';
break;
case 'PURGE':
//Here Handle PURGE Request
echo 'You are using '.$method.' Method';
break;
case 'LOCK':
//Here Handle LOCK Request
echo 'You are using '.$method.' Method';
break;
case 'UNLOCK':
//Here Handle UNLOCK Request
echo 'You are using '.$method.' Method';
break;
case 'PROPFIND':
//Here Handle PROPFIND Request
echo 'You are using '.$method.' Method';
break;
case 'VIEW':
//Here Handle VIEW Request
echo 'You are using '.$method.' Method';
break;
Default:
echo 'You are using '.$method.' Method';
break;
}
?>
I used this code. It should work.
function get_request_method() {
$request_method = strtolower($_SERVER['REQUEST_METHOD']);
if($request_method != 'get' && $request_method != 'post') {
return $request_method;
}
if($request_method == 'post' && isset($_POST['_method'])) {
return strtolower($_POST['_method']);
}
return $request_method;
}
This above code will work with REST calls
and will also work with html form
<form method="post">
<input name="_method" type="hidden" value="delete" />
<input type="submit" value="Submit">
</form>