1

I have PHP application try to get post JSON in 'php://input' using 'file_get_contents' function. Only empty string return. Below is the full code line.

$_PUT = json_decode(file_get_contents("php://input"), true);

Or i need to change any apache/php settings?

4
  • 2
    How are you constructing the request? Commented Oct 24, 2012 at 7:03
  • 3
    From the Doc: "php://input is not available with enctype="multipart/form-data"." Commented Oct 24, 2012 at 7:03
  • Request are using Angular $resource and data in JSON format. So @TobiasSpringer , i need to modified the header while sending the post? im using this header 'Content-Type': 'application/x-www-form-urlencoded' Commented Oct 24, 2012 at 7:05
  • If you're PUTing JSON, you should use 'Content-Type': 'application/json'. Make sure you track the request, for example in google chrome network tab, to ensure you are actually sending a request body Commented Oct 24, 2012 at 9:57

2 Answers 2

1

im using this header 'Content-Type': 'application/x-www-form-urlencoded'

If your data is application/x-www-form-urlencoded, then you should access it via $_POST and not touch the raw HTTP request body. You only do that if you are using an atypical content-type (such as application/json).

If your data isn't in that format, then you should use the correct Content-Type header.

Sign up to request clarification or add additional context in comments.

1 Comment

Still battling, the proposed solution not working. Checked the header thoroughly and it sending using the exactly as per code.
0

Try to clear cache of php and server. Then try another solution:

$_PUT = json_decode(stream_get_contents(STDIN));

I hope it will fix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.