How to find request type in php

When working with external services, like API, we need to debug request type from external URL. Sometimes debugging in Javascript also need to find request type.

In PHP, there is $_SERVER global variable that returns server details with request details. You can catch request type with $_SERVER['REQUEST_METHOD'] variable. Here is example code of how to find request type.

<?php

$method = $_SERVER['REQUEST_METHOD'];

if ($method == 'POST') {
    // POST method
} elseif ($method == 'GET') {
    // GET method
} elseif ($method == 'PUT') {
    // PUT method
} elseif ($method == 'DELETE') {
    // DELETE method
} elseif ($method == 'PATCH') {
    // PATCH method
} elseif ($method == 'OPTIONS') {
    // OPTIONS method
} elseif ($method == 'COPY') {
    // COPY method
} elseif ($method == 'UNLINK') {
    // UNLINK method
} elseif ($method == 'LINK') {
    // LINK method
} elseif ($method == 'PURGE') {
    // PURGE method
} elseif ($method == 'LOCK') {
    // LOCK method
} elseif ($method == 'UNLOCK') {
    // UNLOCK method
} elseif ($method == 'VIEW') {
    // VIEW method
} else {
    // unknown method
}

There is also another method to find request type. It returns same response as above. You can use filter_input() method to find out request type.

$request = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_ENCODED);
Tags:

Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

Or

No Comment