0

I am using below syntax to post some json Data to my URL

curl -H 'Content-Type:application/json' -X POST -d '{"username":"davidwalsh","password":"something"}' http://myProjectURL

i would like to get this json string in array in my php function

but currently when i try to print the post variable like

echo "<pre>";
print_r($post);
die();

it gives me output 
<pre>Array
(
    ['] => Array
        (
            [{username:davidwalsh,password:something}] =>
        )

)

expected Output :

 Array(
        "username" => "davidwalsh",
        "password" => "something"
    )

I am referring this

Though my question is marked as duplicate but i would like to inform you that i am not able to get the each parameter i have sent through json . I am getting all the array key values as a single array key which is absolutely wrong.

Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70
  • Possible duplicate of [How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?](http://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest) – TIGER Feb 06 '17 at 11:08

1 Answers1

0

Finally Got the Answer :

curl -X POST -H 'Content-Type:application/json' -d "{\"username\":\"davidwalsh\",\"password\":\"something\"}" http://myProjectURL

i had to put Double Quotes(") around each parameter and the values with some modification in php file like below.

<?php
echo "<pre>";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $data = json_decode(file_get_contents("php://input") , true);
    print_r($data);
}
?>

And the output i received is :

<pre>Array
(
    [username] => davidwalsh
    [password] => something
)
Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70