Retrieve a post value from IONIC in PHP

0

Good morning,

I'm doing tests with IONIC, and I try to do a REST API in PHP. The value is sent from the app through the following call:

     return this.http.post(this.url + '/', body, reqOpts);

The object I want to recover is the body, according to the browser console:

     console.log(body):
     // respuesta:
     Object { email: "[email protected]", password: "test" }

I do not know how to recover the object, and I have tried:

        header('Access-Control-Allow-Origin: *');
        $header = $_SERVER['HTTP_ORIGIN']; // Esto devuelve correctamente Header: http://localhost:8100
        $password = filter_var($_REQUEST['password'], FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
        $input = json_decode(file_get_contents('php://input'),true);

Both $ password and $ input are empty, they do not even contain arrays. How can I recover these values?

Thanks in advance.

    
asked by noIdea 19.11.2017 в 13:53
source

1 answer

0

Well I do not know if you are using any framework with php I would recommend SLIM this is the way I sent the post

$app->post('/api/cliente', function($request) {
  $data = $request->getParsedBody();

try {

  if(isset($data['clienteid']) && isset($data['cliente'])){
    $codigo= $data['clienteid'];
    $nombre = $data['cliente'];
    $contacto= $data['contacto'];
    $telefono= $data['telefono'];
    $identidad= $data['identidad'];
    $diavisita= $data['diavisita'];
    $ciudad= $data['ciudad'];
    $barrio= $data['barrio'];
    $referencia= $data['referencia'];
    $rutaid= $data['rutaid'];
    $sucursalid= $data['sucursalid'];
    $estado= $data['estado'];
    $longitud= $data['longitud'];
    $latitud= $data['latitud'];
    $celular= $data['celular'];
    $usuariid = $data['usuarioid'];
    $bloque = $data['bloque'];
    $calle = $data['calle'];
    $casa = $data['casa'];

    $sql = "
            insert into mobile_app_cliente
            (codigo,nombre,contacto,telefono,identidad,diavisita,ciudad,barrio,referencia,rutaid,sucursalid,estado,longitud,latitud,celular,usuarioid,bloque,calle,casa)
            values('$codigo','$nombre','$contacto','$telefono','$identidad','$diavisita','$ciudad','$barrio','$referencia','$rutaid','$sucursalid','$estado','$longitud','$latitud','$celular','$usuariid','$bloque','$calle','$casa')
    ";

  //  print_r($sql);
    $db= new db();
    $db = $db->connect();
    $stmt = $db->query($sql);
      $db = null;


  }else {
     echo '{"error":{"text":"Bad Request"}}';
  }


} catch (PDOException $e) {
     echo '{"error":{"text":'.$e->getMessage().'}}';
}


});
    
answered by 27.11.2017 в 17:03