Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

PHP - how to detect http request type in PHP (GET, POST, PUT, DELETE)

0 contributions
10 points

In PHP it is possible to detect request method in following way.

1. Server request method parameter example

backend.php file:

<?php

	$method = $_SERVER['REQUEST_METHOD'];
	
	function parseInput()
	{
		$data = file_get_contents("php://input");
		
		if($data == false)
			return array();
		
		parse_str($data, $result);
		
		return $result;
	}

	switch ($method)
	{
		case 'GET':
			echo "GET request method\n";
			echo print_r($_GET, true);
			break;
		case 'POST':
			echo "POST request method\n";
			echo print_r($_POST, true);
			break;
		case 'PUT':
			$_PUT = parseInput();
		
			echo "PUT request method\n";
			echo print_r($_PUT, true);
			break;
		case 'DELETE':
			$_DELETE = parseInput();
		
			echo "DELETE request method\n";
			echo print_r($_DELETE, true);
			break;
		default:
			echo "Unknown request method.";
			break;
	}

Note: $_PUT and $_DELETES arrays are not supported in php it means php://input stream should be parsed.

ajax.htm file:

<!doctype html>
<html lang="en">
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<pre id="response"></pre>
<script>

	var handle = document.getElementById('response');

	$.ajax({
		type: 'GET', // 'POST', 'PUT', 'DELETE'
		url: '/backend.php',
		data: {
			name: 'John',
			age: 25
		},
		success: function (data) {
			handle.innerHTML = 'Response:\n' + data;
		},
		error: function(error) {
			handle.innerText = 'Error: ' + error.status;
		}
	});

</script>
</body>
</html>

Note: ajax.htm and backend.php should be placed on php server both.

Result:

GET request method example in PHP
GET request method example in PHP - Google Chrome Developer Tools
POST request method example in PHP - Google Chrome Developer Tools
POST request method example in PHP - Google Chrome Developer Tools
PUT request method example in PHP - Google Chrome Developer Tools
PUT request method example in PHP - Google Chrome Developer Tools
DELETE request method example in PHP - Google Chrome Developer Tools
DELETE request method example in PHP - Google Chrome Developer Tools

References

  1. file_get_contents function - PHP Docs
  2. parse_str function - PHP Docs
  3. $_SERVER array - PHP Docs
  4. $_GET array - PHP Docs
  5. $_POST array - PHP Docs
  6. php://input stream - PHP Docs
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more