Saving the contents of php: // input to a variable

I am trying to change and configure another REST server in PHP. It is based on a REST Server written by Phil Sturgeon. I really liked everything, but my requests are not working properly.

The server constructor contains the code

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

I know that php://input can only be read once, so var_dump(file_get_contents('php://input')) before setting the variables shows that my XML data is correctly read from the input stream, but obviously the variables not installed correctly.

But when executing var_dump($this->request->body) only NULL output! Is there a special way to store php://input content in a variable?

EDIT:

I use the Kitchen API to send a POST request and the headers it sends,

 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 

I can not understand what encoding is.

EDIT 3:

The encoding is application/x-www-form-urlencoded , which may be where the problem is! How specifically to say what it should be?

EDIT 2:

$this->request->method contains 'post'

+4
source share
2 answers

Thanks for all the help, it turns out that for the request content type to be application / xml, and not application / x-www-form-urlencoded, as it was.

+4
source

if $this->request->format evaluates to false or NULL or 0 , the later part of the and statement is not executed.

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

You should write it like

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

This helps with debugging.

+1
source

Source: https://habr.com/ru/post/1395748/


All Articles