How can I detect which request type was used(GET, POST, PUT or DELETE) in php?
+1 to that - when in doubt, var_dump($_SERVER) and the answer often lies within.
Paul Dixon
2008-12-11 11:35:25
True but a google search didn't turn up any results, and now within a day or two it will. ;)
Unkwntech
2008-12-11 11:41:25
What happens if you POST to mypage.php?var=something ?
nickf
2008-12-11 12:21:48
The method will be POST, but if you have to use $_GET to get those variables Im not sure.
OIS
2008-12-11 12:50:30
In the case nickf mentions, you could also (maybe) use the $_REQUEST variable. $_REQUEST contains post, get, and cookie values. Read the documentation at http://php.net/manual/en/reserved.variables.request.php
gnud
2008-12-11 23:42:48
@nickf - the variable 'var' from your example will be in `$_POST['var']`
Nathan Long
2010-03-10 16:48:50
http://php.net/manual/en/reserved.variables.server.php
Xiong Chiamiov
2010-09-10 19:45:45
+6
A:
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 = split("/", substr(@$_SERVER['PATH_INFO'], 1));
switch ($method) {
case 'PUT':
rest_put($request);
break;
case 'POST':
rest_post($request);
break;
case 'GET':
rest_get($request);
break;
case 'HEAD':
rest_head($request);
break;
case 'DELETE':
rest_delete($request);
break;
case 'OPTIONS':
rest_options($request);
break;
default:
rest_error($request);
break;
}
?>
neu242
2009-05-22 10:52:06
If you want to have your API available, without quoting what interpreting engine you're using, add a .htaccess file containingRewriteEngine onRewriteRule ^api/(.*)$ api.php/$1This 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.
JonTheNiceGuy
2010-07-01 11:55:59