1

I am trying to post json data to php file using linux curl command, (Lamp Server)

$ curl -V -H "Content-Type: application/json" -X POST -d '{"id": "123"}' 
       http://localhost/crm/UpdateUser.php

In UpdateUser.php,

<?php echo var_dump ($_POST);?>

OUTPUT:

[ec2-user@ip-10-35-1-181 ~]$ curl -v -H "Content-Type: application/json" -X POST -d '{"id": "123"}' http://viacrm.odema.net/crm/UpdateUser.php

* Hostname was NOT found in DNS cache
*   Trying 54.217.206.217...   
> POST /crm/UpdateUser.php HTTP/1.1
> User-Agent: curl/7.36.0
> Host: 192.168.1.16
> Accept: */*
> Content-Type: application/json
> Content-Length: 13
>
* upload completely sent off: 13 out of 13 bytes
< HTTP/1.1 200 OK
< Date: Mon, 16 Jun 2014 12:25:00 GMT
* Server Apache/2.2.27 (Amazon) is not blacklisted
< Server: Apache/2.2.27 (Amazon)
< X-Powered-By: PHP/5.3.28
< Content-Length: 13
< Connection: close
< Content-Type: text/html; charset=UTF-8
<
array(0) {
}
* Closing connection 0

Always the Post data shows empty, I even tried to use "ACCEPT: application/json", still same problem. Please can anyone guide this ?

4
  • have you tired without -X POST options? Commented Jun 16, 2014 at 12:56
  • yes i have tried without -x post Commented Jun 16, 2014 at 12:58
  • hope it help you: stackoverflow.com/questions/7172784/… Commented Jun 16, 2014 at 13:02
  • even that links curl -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' localhost/crm/UpdateUser.php returns empty array Commented Jun 16, 2014 at 13:08

2 Answers 2

3

$_POST only contains the results of decoding an application/x-www-form-urlencoded request. You need to read the raw request body. If you have the always_populate_raw_post_data configuration directive turned on, then the raw body will be in $HTTP_RAW_POST_DATA; otherwise you can obtain it by reading from the php://input stream.

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

Comments

0

Instead of $_POST try it:

<?php
    print($HTTP_RAW_POST_DATA);
?>

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.