【问题标题】:Cant get JSON POST data submitted via Slim无法获取通过 Slim 提交的 JSON POST 数据
【发布时间】:2013-12-02 03:51:26
【问题描述】:

我正在使用 Postman(在 Chrome 中)测试 Slim 调用,但不知道如何获取任何已发布的 JSON 数据。

我正在提交原始 JSON:

{"name":"John Smith", "age":"30", "gender":"male"}

在标头中使用 Content-Type: application/json

通过 POST 到:

http://domain/api/v1/users/post/test/

每次获取 JSON 数据的尝试都会出现致命错误(参见下面的代码 cmets)

<?php
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->add(new \Slim\Middleware\ContentTypes());


$app->group('/api/v1', function () use ($app) {

    $app->group('/users/post', function () use ($app) {

        $app->post('/test/', function () {

                print_r($app->request->headers); //no errors, but no output?

                echo "Hello!"; // outputs just fine

        $data = $app->request()->params('name'); //Fatal error: Call to a member function request() on a non-object
                $data = $app->request->getBody(); //Fatal error: Call to a member function getBody() on a non-object
                $data = $app->request->post('name'); //Fatal error: Call to a member function post() on a non-object
                $data = $app->request()->post(); //Fatal error: Call to a member function request() on a non-object

                print_r($data);
                echo $data;

        });

    });

});

$app->run();
?>

我错过了什么?

谢谢!

【问题讨论】:

  • 尝试var_dump($app) 出现第一个致命错误的地方。也许尝试使用 $this 而不是 $app。
  • var_dump($app) 结果为 NULL
  • $this->request()->getBody(); = 致命错误:不在对象上下文中使用 $this

标签: php post slim


【解决方案1】:

确保将$app curry 到最后一个嵌套路由中,如下所示:

// Pass through $app
$app->post('/test/', function () use ($app) {

你在其他任何地方都这样做,所以我假设你只是忽略了它。

【讨论】:

  • 谢谢,但是 Slim 文档在组中只有“use ($app)”而不是实际路线:docs.slimframework.com/#Route-Groups
  • 那是因为示例在这些路由中没有使用$app。你只咖喱你真正需要的东西。我们在这里讨论的是 PHP 语言要求,而不是 Slim 要求:“闭包也可以从父作用域继承变量。任何此类变量都必须传递给 use 语言构造。” us1.php.net/manual/en/functions.anonymous.php
  • 有效!谢谢!但是,天哪。这可能是我遇到的第 10 个问题,因为文档的代码/信息不准确。
  • @Ahhk:糟糕的文档可能会让人非常沮丧,但在这种情况下,作者假设对 PHP 的匿名函数有所了解。在其他 9 种情况下,如果您有时间和意愿,如果您针对文档存储库提出问题,那就太棒了:github.com/codeguy/Slim-Documentation/issues
  • 我是 Slim/REST 的新手,所以我只是通过示例进行,并没有真正分析它。另一个例子:文档显示将中间件添加为“$app->add(new Slim_Middleware_ContentTypes())”的代码。这不起作用。它必须像我上面所说的那样(经过广泛的搜索和拉头发)。 :)
【解决方案2】:

你必须从请求中获取正文:

$app->request->getBody();

http://docs.slimframework.com/request/body/

【讨论】:

    【解决方案3】:

    在 Slim 3 中,我在 curl POST 数据中使用了 body 的值,但它不起作用, 为了解决这个问题,我使用了这个,主体是对象而不是字符串:

    $app->post('/proxy', function($request, $response) {
        $data = $request->getBody()->getContents();
        $response->getBody()->write(post('http://example.com', $data));
    });
    

    您可以在docs查看更多关于body的方法

    【讨论】:

      【解决方案4】:
          //You did not pass the ($app) in your post route... so make it correct 
          $app->post('/test/', function () use ($app){
              //now if you want to get json data so please try following code instead of print_r($app->request->headers);
              $us=$app->request->getbody(); 
      
              //$us will get the json data now if you want to decode it and and want to get an array so try following..
              $ar=json_decode($us,true);
              //now you have $ar array of json data use it where you want..
          });
      

      【讨论】:

        【解决方案5】:

        试试这个:

        $app->post('/test/', function () use ($app){
                instead of print_r($app->request->headers);
                $ar=$app->request->getbody(); 
        
               to get an array so try following..
                $arry=json_decode($ar,true);
        
            });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-24
          • 2015-03-11
          • 2019-06-03
          • 1970-01-01
          • 2021-10-30
          相关资源
          最近更新 更多