1

I am passing parameters by POST which are encoded in Json. I have captured the parameters in a PHP file. The JSON encoded parameters are as follows:

{
    "id": "5",
    "name": "abcd",
    "imei": "1234"
}

First I have captured them as follows:

$entityBody = file_get_contents('php://input');

Then I have decoded them as follows:

$entityBody = json_decode($entityBody, true);

Now I can access id as follows:

 $entityBody['id']

I want to know say, if any parameter say, 'description' is present in POST. If anyone helps me how to do that I will be really grateful. Thank you.

Joy
  • 4,197
  • 14
  • 61
  • 131
  • can you use [`isset`](http://php.net/manual/en/function.isset.php)? like `if(isset($_POST['something'])) ...` – naththedeveloper Jul 23 '13 at 07:26
  • 1
    maybe you mean `$entityBody = json_decode($_POST['json'], true);` but anyway everything works fine for, what is the problem? access it like `$entityBody['description']` – vladkras Jul 23 '13 at 07:30
  • what do you want to know, if _'description' is present in POST_ or _'description' is present in $entityBody_? – vladkras Jul 23 '13 at 07:42
  • @jcsanyi I just tried to clarify his question. It seems to me, your answer is confusing, not my coment. – vladkras Jul 23 '13 at 07:49

2 Answers2

1

Use isset to check whether 'description' is present in POST or not

if(isset($_POST['description'])) {
     //your code here
}

Edit

As you are using file_get_contents('php://input');, so you have to use

if (isset($entityBody['description'])) {
    //your code here
}
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • Please re-read the full question. Since the post parameters are encoded as JSON, they're not automatically interpreted by PHP, and will not be in the `$_POST` array. – jcsanyi Jul 23 '13 at 07:30
  • @jcsanyi I think you haven't read the question properly, OP is saying `'description' is present in POST` not in JSON. :) – Yogesh Suthar Jul 23 '13 at 07:31
  • In POST is not the same as in $_POST. – jcsanyi Jul 23 '13 at 07:33
  • @YogeshSuthar I have one confusion regarding this. One thing I just observed that the code is working properly if keys are in double quotes as shown above, but it is not working when keys are in single quote for example if it is in the form 'id': "id value". Could you kindly advise how to handle that? – Joy Jul 23 '13 at 07:58
0

This really is as simple as most of the other answers are saying - but you just need to check your $entityBody array, not the $_POST array.

if (isset($entityBody['description'])) {
    // do something
}

To clarify for those that are still confused, if POST'ed data is encoded in one of the standard HTML form encodings (application/x-www-form-urlencoded or multipart/form-data), it will be automatically decoded and available in the $_POST array.

In this case, the OP is POST'ing data in a custom (json) format instead. That means the $_POST array will be empty, and they have to manually read and decode the POST contents themselves - which it appears they've already got working.

See also: What is the Raw Post Data

Community
  • 1
  • 1
jcsanyi
  • 8,133
  • 2
  • 29
  • 52