Receive json post in php from browser

232 Views Asked by At

I am trying to receive and print json with this php code:

<?php

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);

?>

I am not receiving or printing any data. But if i use this ajax:

<script    src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">    </script>
<script>
(function($){
    function processForm( e ){
        $.ajax({
               url:'http://mydyndns:8010/has/input_parameters_startReg.php',
            dataType: 'json',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({ DeviceID: 23, ForceReg: 0, StartTime: "10/06/2015 17:45"}),
             success: function (data) {
        },
        error: function (e) {
            alert(e.responseText);
        },
    });

        e.preventDefault();
    }

    $('#my-form').submit( processForm );
})(jQuery);

it is working, and i get the posted data printed in my browser. How can i alter my php so the direct hitting from browser will give me the result the ajax gives?

2

There are 2 best solutions below

2
On BEST ANSWER

Function parse_str will do the job in combination with $_SERVER['QUERY_STRING']

<?php
 parse_str($_SERVER['QUERY_STRING'], $output);
 $json = json_encode($output);
 echo $json;
?>
0
On

I don't think you'll be able to fetch it using GET because php://input seems to only read POST data. If you want to be able to use the GET data, you can refer to the $_GET global:

print_r($_GET);

Note that you can also use $_POST in place of php://input, but here's a question/answer on here that talks about that some:

PHP "php://input" vs $_POST

EDIT: Oops, I see this was answered while I was taking my sweet time.