I’m trying to send some JSON data over POST with fopen, using the function below.
function doPostRequest($url, $data, $optional_headers = null)
{
    $params = array(
        'http' => array(
            'method'  => 'POST',
            'content' => $data,
            'header'  => 'Content-type: application/json' . "\r\n"
                       . 'Content-Length: ' . strlen($data) . "\r\n"
        )
    );
    if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
    }
    $ctx = stream_context_create($params);
    try {
        $fp = fopen($url, 'rb', false, $ctx);
        $response = stream_get_contents($fp);
    } catch (Exception $e) {
        echo 'Exception: ' . $e->getMessage ();
    }
    return $response;
}
$json_data = array(
    'first_name' => 'John',
    'last_name'  => 'Doe'
);
$content = urlencode(json_encode($json_data));
doPostRequest($url, $content);
I don’t get any error, but when I’m trying to decode the data, $input_stream is empty. Why? What am I missing?
$input_stream = file_get_contents('php://input');
$json = urldecode($input_stream);
var_dump($input_stream);
Edit: I should mention that the code works on a machine with XAMPP installed, but it doesn’t on another one. Could the server configuration have anything to do with it?
 
     
    