A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearDetecting request type in PHP (GET, POST, PUT or DELETE) - page 1
User InfoPosts
Detecting request type in PHP (GET, POST, PUT or DELETE)#1
How can I detect which request type was used (GET, POST, PUT or DELETE) in PHP?

posted date: 2008-12-11 03:31:00


Re: Detecting request type in PHP (GET, POST, PUT or DELETE)#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-11 03:31:01


Re: Detecting request type in PHP (GET, POST, PUT or DELETE)#3
By using$_SERVER['REQUEST_METHOD']For more details please see the documentation for the $_SERVER variable.

posted date: 2008-12-11 03:32:00


Re: Detecting request type in PHP (GET, POST, PUT or DELETE)#4
+1 to that - when in doubt, var_dump($_SERVER) and the answer often lies within.

posted date: 2008-12-11 03:35:00


Re: Detecting request type in PHP (GET, POST, PUT or DELETE)#5
True but a google search didn't turn up any results, and now within a day or two it will. ;)

posted date: 2008-12-11 03:41:00


Re: Detecting request type in PHP (GET, POST, PUT or DELETE)#6
What happens if you POST to mypage.php?var=something ?

posted date: 2008-12-11 04:21:00


Re: Detecting request type in PHP (GET, POST, PUT or DELETE)#7
don't forget HEAD =) (also OPTIONS, TRACE and CONNECT, but I don't think PHP ever gets those).

posted date: 2008-12-12 15:22:00


Re: Detecting request type in PHP (GET, POST, PUT or DELETE)#8
REST in PHP can be done pretty simple. Create http://example.com/test.php (outlined below). Use this for REST calls, e.g. http://example.com/test.php/testing/123/hello. This works with Apache and Lighttpd out of the box, and no rewrite rules are needed.<?php$method = $_SERVER['REQUEST_METHOD'];$request = explode("/", substr(@$_SERVER['PATH_INFO'], 1));switch ($method) { case 'PUT': do_something_with_put($request); break; case 'POST': do_something_with_post($request); break; case 'GET': do_something_with_get($request); break; case 'HEAD': do_something_with_head($request); break; case 'DELETE': do_something_with_delete($request); break; case 'OPTIONS': do_something_with_options($request); break; default: handle_error($request); break;}

posted date: 2009-05-22 02:52:00


Re: Detecting request type in PHP (GET, POST, PUT or DELETE)#9
If you want to have your API available, without quoting what interpreting engine you're using, add a .htaccess file containing RewriteEngine on RewriteRule ^api/(.*)$ api.php/$1 This assumes your API file is called api.php. Also, since the above code block was written, the PHP developers have depreciated the split function. it works fine if you replace split with explode.

posted date: 2010-07-01 03:55:00


Re: Detecting request type in PHP (GET, POST, PUT or DELETE)#10
@NathanLong In my experience that is incorrect. If you POST to mypage.php?var=something then 'something' will be in $_GET['var'].

posted date: 2012-02-06 20:51:00


Re: Detecting request type in PHP (GET, POST, PUT or DELETE)#11
What's with the @ in front of $_SERVER['PATH_INFO']?

posted date: 2013-04-19 02:48:00


Re: Detecting request type in PHP (GET, POST, PUT or DELETE)#12
@Svish, what a great detail you noticed! It gets rid of PHP Notice: Undefined index: PATH_INFO in case PATH_INFO is not in $_SERVER. I'm adding this to my bag of tricks right away! It's a way of saying "I know there might not be an entry named that way in this array, and I'm ready for that, so just shut up and do what I tell you to". :) Thanks guys, both for posting this answer and for bringing my attention to that particular character in it.

posted date: 2013-06-04 04:24:00


select page: « 1 2...»
Copyright ©2008-2017 www.momige.com, all rights reserved.