Get PATCH request data in PHP

2020-06-07 06:38发布

I need to make a PATCH request to a PHP application.

How can I get the data from that PATCH request inside that application?

If I had to do it with a POST, it would just be a simple access to the global $_POST variable.

标签: php rest
3条回答
地球回转人心会变
2楼-- · 2020-06-07 07:03

You can get data with php://input stream wrapper:

$data = file_get_contents('php://input');

Also make sure your web server supports PATCH requests, some are configured to respond only to GET and POST.

查看更多
戒情不戒烟
3楼-- · 2020-06-07 07:10

I know that this has been solved, but for anyone who was hoping for an answer like

$_PATCH["name"];

there is a way to do that:

parse_str(file_get_contents('php://input'), $_PATCH);

then you can access it like $_GET["something"] and $_POST["something"] just do

$_PATCH["something"]

hope that helped someone :)

查看更多
来,给爷笑一个
4楼-- · 2020-06-07 07:27

You have $_REQUEST superglobal containing all data we can get regardless the HTTP method used (GET, POST, PATCH, PUT)

查看更多
登录 后发表回答