【问题标题】:storing contents of php://input in a variable将 php://input 的内容存储在变量中
【发布时间】:2012-03-02 20:34:26
【问题描述】:

我正在尝试用 PHP 编辑和调整别人的 REST 服务器。它基于 Phil Sturgeon 编写的REST Server。我几乎完全理解了这一切,但我的请求没有按预期工作。

在服务器构造函数中是代码

switch ($this->request->method)
{
    case 'post':
    $this->_post_args = $_POST;
    $this->request->format and $this->request->body = 
                                    file_get_contents('php://input');
    break;
}

我知道php://input 只能读取一次,所以在设置变量之前执行var_dump(file_get_contents('php://input')) 表明我的 XML 数据正在从输入流中正确读取,但显然变量设置不正确。

但是var_dump($this->request->body) 只会输出NULL!有没有什么特殊的技术可以将php://input的内容存储在一个变量中?

编辑:

我正在使用API Kitchen 发送 POST 请求,它发送的标头是

Status: 200
X-Powered-By: PHP/5.3.2-1ubuntu4.11
Server: Apache/2.2.14 (Ubuntu)
Content-Type: application/xml
Date: Fri, 10 Feb 2012 11:00:43 GMT
Keep-Alive: timeout=15, max=100
Content-Length: 936
Connection: Keep-Alive

我看不出编码是什么。

编辑 3:

编码是application/x-www-form-urlencoded,这可能是问题所在!我该如何具体说这应该是什么?

编辑 2:

$this->request->method 包含'post'

【问题讨论】:

  • $this->request是什么类型的对象?
  • $this->request 被声明为protected $request = NULL; // Stores accept, language, body, headers, etc
  • 我找不到单独的实现或任何东西,第一次设置是在同一个构造函数中,并且设置为$this->request->method = $this->_detect_method();。抱歉,我在这里似乎不是很有帮助!
  • 通过插入打印语句进行调试。并分解复合语句。
  • php://input 不适用于 multipart/formdata 但您提到情况并非如此。然后你说$this->request->format 打印NULL,你实际上是在做类似null and $a = 1; 的事情,这意味着$a = ...; 部分根本不执行!

标签: php codeigniter rest http-headers


【解决方案1】:

感谢大家的帮助,事实证明,为了工作,请求的内容类型必须是 application/xml,而不是原来的 application/x-www-form-urlencoded。

【讨论】:

    【解决方案2】:

    如果$this->request->format 的计算结果为falseNULL0,则and 运算符的后半部分不会执行。

      $this->request->format and $this->request->body = file_get_contents('php://input');
                                 ^
                                 |
                                 +--- this part wont execute
    

    你应该这样写

    if($this->request->format){
        $this->request->body = file_get_contents('php://input');
    }
    

    这有助于调试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-08
      • 2015-09-20
      • 2016-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 2014-01-18
      相关资源
      最近更新 更多