解析POST参数 - PUT不/ LAMP服务器

时间:2013-07-08 21:52:49

标签: php apache rest post put

我在将参数传递给LAMP服务器时遇到了困难:

  • Apache / 2.2.22(Ubuntu)
  • PHP 5.3.10-1ubuntu3.6
  • 卷曲7.22.0

在服务器端,我使用slim来进行REST操作。它似乎适用于GET / POST。我的测试实现如下:

 // Check the post route
 $app->post('/data', function () use ($app) {
        $app->response()->header("Content-Type", "application/json");
        $json_new_array["input"] = file_get_contents('php://input'); 
        $json_new_string = json_encode($json_new_array);
        echo $json_new_string;
    });

 // Check the put route
 $app->put('/data', function () use ($app) {
         $app->response()->header("Content-Type", "application/json");
         $json_new_array["input"] = file_get_contents('php://input'); 
         $json_new_string = json_encode($json_new_array);
         echo $json_new_string;
    });

以下是我在客户端尝试传递参数的原因:

curl -X PUT http://hostname/001_mockserver.php/data -d fruit=orange -d quantity=4 -i

curl -X POST http://hostname/001_mockserver.php/data -d fruit=orange -d quantity=4 -i

PUT 尝试在{"input":""}中返回,而 POST 行为符合预期:{"input":"fruit=orange&quantity=4"}

我读到apache应该不是问题。那么任何建议从哪里开始?

1 个答案:

答案 0 :(得分:0)

好吧,我已经明白了:

// Check the put route
$app->put('/data', function () use ($app) {
         $request = $app->request();
         $body = $request->getBody();
  });

完成工作:-D