How can I find out the http request method from PHP?

Asked

Viewed 654 times

2

I know that every http request made, whether by browser or other resource, sends a certain method (GET, POST, PUT, DELETE and etc.).

I’m thinking about building a Restful application with PHP, but I don’t know how I can find out what the current request method is, so I can validate it.

If it is a requisition GET in a url that accepts only POST, I want to return the Error 405, which is the método não aceitável.

How to know the current PHP request method?

  • A useful tip: Make a simple application that gives a var_dump($_REQUEST). There’s quite a useful thing there. (-:

  • @Perrywerneck $_REQUEST or $_SERVER?

  • Oops! In this case $_SERVER. (-:

1 answer

2

You can use the following code:

if( $_SERVER['REQUEST_METHOD'] == 'POST'){
  //retorne 405;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.