1

I am firing a curl request from command line.

My curl request looks like:

curl -X POST -H "Accept: application/json"  -H "Content-type: application/json"  -d '{ "BookingByCustomer" : "testUser", "BookingDate" : "11111111", "TotalCost" : "11", "NetAmount"  : "11" }' http://serverIP:port/test.php 

My PHP code:

    <?php
   /** global variables */
    $request_called = ($_SERVER['REQUEST_METHOD'] == 'GET') ? 'GET' : 'POST';

    if($request_called == 'POST')
    {
         handle_post_request();
    }
    if($request_called == 'GET')
     {
       handle_get_request();
     }

    function handle_get_request (){
      echo "Get Request has been called!";   
   }

    function handle_post_request (){
   $json = $_SERVER['HTTP_JSON'];
    print_r ($_SERVER);
   }
 ?>

but $_SERVER doesn't seems to have json data. Did i miss something??

4
  • Hard to tell without more code. You just provided use with a function. Commented Mar 26, 2013 at 23:52
  • its faiirly simple php.. but still i post code here Commented Mar 26, 2013 at 23:53
  • I note you look for it in SERVER... shouldn't it go in POST? Commented Mar 26, 2013 at 23:55
  • You should NOT pretend you received a POST when you don't recognize the request method. Instead you should return a 405 HTTP error. Maybe you received a HEAD request... Commented Jun 15, 2020 at 22:49

3 Answers 3

3

There is no such entry as $_SERVER['HTTP_JSON'] in $_SERVER. You need to get the POST content from $_POST.

But as you are not giving your JSON data a variable name you should get the raw POST content with

$json = file_get_contents("php://input");

Another solution would be to assign a variable name to your JSON data:

curl -X POST ... -d 'HTTP_JSON={ ... }'

As long as you have no forbidden characters (like ? or &) in your JSON you are safe, otherwise you would also have to URL encode your JSON string. That's why I suggested the first solution using php://input. This way you don't have to care about URL encoding.

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

4 Comments

Can you please explain a bit your answer. Becasue by defination i got - "file_get_contents() is the preferred way to read the contents of a file into a string." but I am not sending any file.
In php URLs are also treated as files. This one is a PHP specific URL holding the complete body of a request. You are sending your JSON in the body of a POST request so you have to get it this way. I'll also show you another way to do this using the $_POST array in a minute...
one more doubt, If i send data from andriod client, do i need to change the code? or will it work as same..
this will work for every POST request, no matter where it comes from, browser or command line, handset or desktop...
0

You are posting the information with curl so use $_POST instead of $_SERVER.

function handle_post_request (){
    $json = $_POST['HTTP_JSON'];
    print_r ($_POST);
}

2 Comments

i tried but got a blank array ... any suggestion why it is so
$_POST is right, but as the JSON has no variable name you have to get the raw POST data
0

This function returns the value of $_SERVER and not the value of the $json-variable.

  function handle_post_request (){
   $json = $_SERVER['HTTP_JSON'];
    print_r ($_SERVER);
   }

And second I believe you have to use $_POST to get this working:

  function handle_post_request (){
   $json = $_POST['HTTP_JSON'];
    print_r ($json, true);
   }

4 Comments

$_POST is right, but as the JSON has no variable name you have to get the raw POST data
ahh. But then $json = $_POST['HTTP_JSON'] is totally useless?
yes because request doesn't have HTTP_JSON ..so yes it is useless until this name given to Json data.. and I was printing $_SERVER variable in first case so that i can see my all request parameter.
Ah ok thanks! :-) (No I'm not the really the best programmer in the world ;-) But I'm learning :-))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.