Q:

How to get the JSON Object in PHP from Android OKHttp

I have my OkHttp code here (i'm working in Android)
    void postRequest(String postUrl, String postBody) throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON,postBody);
        Request request = new Request.Builder()
            .url(postUrl)
            .post(body)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            call.cancel();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.d("TAG",response.body().string());
        }
    });
}

And this is my PHP part
<?php
    header("Content-type: application/json; charset=utf-8");

    include("conexion.php");

    $nombre = $_POST["nombre"];
    $apellidoPaterno = $_POST['apellidoPaterno'];
    $apellidoMaterno = $_POST['apellidoMaterno'];
    $direccion = $_POST['direccion'];
    $redesSociales = $_POST['redesSociales'];
    $telefono = $_POST['telefono'];
    $nombreUsuario = $_POST['nombreUsuario'];
    $contrasena = $_POST['contrasenaUsuario'];

?>

I want to obtain the values that are passing through my JSON, but when I use $_POST they end with no values. I've tried with the API of reqres and it does send the information.
Any help is appreciated, thanks.

A:

Following your and my comments you could do the following:
<?php
// header("Content-type: application/json; charset=utf-8"); // not really needed here for now
    include("conexion.php");
    $fgc = file_get_contents("php://input");
    $json = json_decode($fgc, true);
    // now you've got all your values in $json:
    $nombre = $json["nombre"];

alternatively you could do:
    $json = json_decode($fgc);
    // now you've got all your values as an object in $json:
    $nombre = $json->nombre;   

further reading: http://php.net/manual/de/wrappers.php.php#wrappers.php.input

