Detecting request type in PHP (GET, POST, PUT or DELETE)

UnkwnTech Source

How can I detect which request type was used (GET, POST, PUT or DELETE) in PHP?



answered 10 years ago gnud #1

By using



     // The request is using the POST method

For more details please see the documentation for the $_SERVER variable.

answered 9 years ago neu242 #2

REST in PHP can be done pretty simple. Create (outlined below). Use this for REST calls, e.g. This works with Apache and Lighttpd out of the box, and no rewrite rules are needed.

$request = explode("/", substr(@$_SERVER['PATH_INFO'], 1));

switch ($method) {
  case 'PUT':
  case 'POST':
  case 'GET':

answered 5 years ago nurettin #3

Since this is about REST, just getting the request method from the server is not enough. You also need to receive RESTful route parameters. The reason for separating RESTful parameters and GET/POST/PUT parameters is that a resource needs to have its own unique URL for identification.

Here's one way of implementing RESTful routes in PHP using Slim:

$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
  echo "Hello, $name";

And configure the server accordingly.

Here's another example using AltoRouter:

$router = new AltoRouter();
$router->setBasePath('/AltoRouter'); // (optional) the subdir AltoRouter lives in

// mapping routes
$router->map('GET|POST','/', 'home#index', 'home');
$router->map('GET','/users', array('c' => 'UserController', 'a' => 'ListAction'));
$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');

answered 3 years ago Amit Patange #4

$request = new \Zend\Http\PhpEnvironment\Request();
$httpMethod = $request->getMethod();

In this way you can also achieve in zend framework 2 also. Thanks.

answered 3 years ago Peter #5

Detecting the HTTP method or so called REQUEST METHOD can be done using the following code snippet.

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 an hidden field in the form.

<!-- DELETE method -->
<form action='' method='POST'>
    <input type="hidden" name'_METHOD' value="DELETE">

<!-- PUT method -->
<form action='' method='POST'>
    <input type="hidden" name'_METHOD' value="PUT">

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

answered 3 years ago Rockers Technology #6

You can get any query string data i.e

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.

answered 2 years ago Artegon #7

You can use getenv function and don't have to work with a $_SERVER variable:


More info:

answered 2 years ago HelpNeeder #8

We can also use the input_filter to detect the request method while also providing security through input sanitation.


answered 1 year ago Juned Ansari #9

It is Very Simple just use $_SERVER['REQUEST_METHOD'];


switch ($method) {
  case 'GET':
    //Here Handle GET Request 
  case 'POST':
    //Here Handle POST Request 
  case 'DELETE':
    //Here Handle DELETE Request 
  case 'PUT':
    //Here Handle PUT Request 

answered 10 months ago Thielicious #10

When a method was requested, it will have an array. So simply check with count().

foreach($m as$k=>$v){
    echo count($v)?
    $k.' was requested.':null;

comments powered by Disqus