Php $ _SERVER [" REQUEST_METHOD"]总是返回get [英] Php $_SERVER["REQUEST_METHOD"] alway return get
问题描述
我在我的localhost中写了一些代码。
I'm writing some code in my localhost.
index.php:
$task = null;
$method = $_SERVER['REQUEST_METHOD'];
var_dump($method);
//initialize data
HttpProtocol::init();
if (empty($method))
exitApp("unknown method");
if ($method == HttpProtocol::get())
$task = new WebhookVerifyTask();
else if ($method == HttpProtocol::post())
$task = new ProcessFacebookEventTask();
if (is_null($task))
exitApp("unknown method");
$task->start();
http_response_code(200);
如果我发送GET或POST请求无关紧要,$ method将始终为GET 。
尝试PUT或DELETE时 - 它会完美地改变..
it doesn't matter if I send a GET or POST request, the $method will always be GET. When trying PUT or DELETE - it changes perfectly..
什么可能导致$方法即使在POST时仍然是GET?
What could cause the $method to always be GET even when POST ?
UPDATE
显然当我将请求发送到localhost / path时 - 出现上述行为。如果我将它发送到localhost / path / - 帖子工作正常。
UPDATE apparently when i'm sending the request to localhost/path - the above behaviour occur. if i'm sending it to localhost/path/ - the post works perfectly.
推荐答案
显然当我将请求发送到localhost / path时 - 会发生上述行为。如果我将它发送到localhost / path / - 帖子工作正常
apparently when i'm sending the request to localhost/path - the above behaviour occur. if i'm sending it to localhost/path/ - the post works perfectly
您的更新也会回答您的问题。如果是/ path,但是没有这样的文件,Web服务器会自动将您重定向到/ path /。 - Janno
执行此重定向时 - 他没有完全使用所有请求数据和方法吗?
when it does this redirection - he does not do it completely with all the request data and methods ?
它不能。 Web服务器决定告诉客户端它应该尝试对另一个URL的另一个请求。 Web服务器使用 302 Found
状态代码和位置:http:// localhost / path /
标头进行响应。这会导致客户端向该新位置发出另一个HTTP请求,并且新请求将始终为 GET
请求。 POST
请求无法重定向。 (好吧,从理论上讲,它们可以使用 307临时重定向
,但实际上并没有广泛支持。)
It cannot. The web server decides to tell the client that it should try another request to a different URL. The web server responds with a 302 Found
status code and a Location: http://localhost/path/
header. This causes the client to make another HTTP request to that new location, and that new request will always be a GET
request. POST
requests cannot be redirected. (Well, theoretically they can be with a 307 Temporary Redirect
, but in practice that is not widely supported.)
您需要直接向规范网址发出请求,以免导致重定向。
You need to make your request to the canonical URL directly so as to not cause a redirect.
这篇关于Php $ _SERVER [" REQUEST_METHOD"]总是返回get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!