3

file_get_contents("php://input"); is not working.

$data=$_GET['json']; is working.

my url:

http://localhost/demo/plainjson.php?json={"order_number":"54321","id":"1102","status":3,"card_no":"1234"}

$data = file_get_contents("php://input");
$json = json_decode($data, true);
var_dump($json);// o/p->Null
print_r($json);// o/p-> nothing

echo $json[2];

how to echo or print the url json array or value using this need help. with explanation. thank you in advance.

3
  • Please ignore last code line echo $json[2]; it's also echo false or nothing i don't know. Commented Aug 24, 2015 at 5:31
  • In the request you put the json in a querystring var called json, therefor, shouldn't the json_decode be on $data['json'] rather than just $data? Commented Aug 24, 2015 at 7:16
  • i am not happy man, for this i have to learn Cake PHP now. Commented Aug 25, 2015 at 5:02

1 Answer 1

1

allow_url_include may not be set to true in your php.ini? This is common on some older shared hosting services

Refer to this Stack Overflow post for a potential fix.

Ahhh... The problem is that you arent POSTing anything. But rather sending it via query string so there is no body for the stream to retrieve. So as I understand it you cannot use a streams to get the data

See PHP.net Aritical

"php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST request"

Further more see this post

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

4 Comments

checked i changed the value from: allow_url_include = off to on in php.ini and restarted the wamp. m using wamp. still not working.
so, can you tell m hw to post data? on the same page where i want use file_get_contents("php://input") function to get values. i knw smwht stupid but.. :)
Well there are a couple of options. You can submit a form with your data in a form field and the form method type to POST. Or you can do it in PHP via cURL. Refer to - davidwalsh.name/curl-post - for more instructions on that.
if your site is https and we submit a POST request of content type JSON using a http url of the site, then you may get php://input as null.when submitting data to a https website using https could be better

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.