PHP Find Request Type

  296 views   9 months ago 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);

I hope it will help you.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]