4

Possible Duplicate:
How to get body of a POST in php?

I'm receiving a POST that contains a JSON, the problem is when I receive that the $_POST is empty. In order to test when I receive the POST I create a file that contais the $_POST, $_GET and $_REQUEST and they are all empty.

The client that is sending the request is doing something like this:

$itemJson = '{
      "id": 00,
      "value": "ok"
    }';

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: '. strlen($itemJson))
    );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $itemJson);
    curl_close($ch);

To be honest I don't understant how I'm getting this data since there's no parameter set.

Any ideas?

Community
  • 1
  • 1
fred00
  • 571
  • 2
  • 8
  • 23
  • Yes, I'm sorry. I did search before posting, but I got nothing. I didn't know how to search for this problem. – fred00 Oct 24 '12 at 15:40
  • I would not feel too bad about it, I cannot find answers on stackoverflow *which I know the title for* sometimes, including my own :) – Maarten Bodewes Oct 25 '12 at 00:30

2 Answers2

18

Try in PHP as below (When request is an application/json, then you will not get data into $_POST)

var_dump(json_decode(file_get_contents("php://input")));
GBD
  • 15,847
  • 2
  • 46
  • 50
5

Try

$request_body = file_get_contents('php://input');
$json = json_decode($request_body);

in your receiving script.

see also: http://docs.php.net/wrappers.php.php

VolkerK
  • 95,432
  • 20
  • 163
  • 226