PHP can not read variable sent from a POST

0

I'm doing POST from angular to a file in PHP but the data that is sent is not being recognized in the file PHP , I have already tried several ways but I still can not read the parameter "op" in file PHP .

The request is sent to the PHP file but the data that is sent by POST does not arrive. If I delete the condition of the if I return the data of the querry. But I need to do the verification with the IF and the variable 'op'.

Angular controller:

public getListStudents() {
    let headers = new Headers();
    headers.append('contentType', 'application/x-www-form-urlencoded');

    return this.http.post("http://localhost/obtener_alunos.php", {op: 1}, 
    headers);
}

PHP

<?php
    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Methods", "POST, GET");
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-
    Type, Accept");

    require("conectar_mysql.php");

    if ((isset($_REQUEST['op'])) && ($_REQUEST['op'] == 1)) {
        $vec_res = array();
        $result = mysqli_query($conn, "SELECT * FROM alunos");

        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_assoc($result)) {
                array_push($vec_res, array('nome' => $row['nome'], 'idade' => 
                $row['idade'], 'morada' => $row['morada']));
            }
            echo json_encode($vec_res);
        }
    }

    mysqli_close($conn);
?>

The problem has already been solved using your suggestions $ request = json_decode (file_get_contents ("php: // input")) - > op; I had to put op to recover the value. What I still do not understand is that the $ _REQUEST or $ _POST does not work.

<?php
    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-
    Type, Accept");

    $request = json_decode(file_get_contents("php://input")) -> op;

    require("conectar_mysql.php");

    if (isset($request) && ($request == 0)) {
        $vec_res = array();
        $result = mysqli_query($conn, "SELECT * FROM alunos");

        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_assoc($result)) {
                array_push($vec_res, array('nome' => $row['nome'], 'idade' => 
                $row['idade'], 'morada' => $row['morada']));
        }
        echo json_encode($vec_res);
    }
}

    mysqli_close($conn);
?>

Thank you all for your time.

    
asked by Reynier Téllez 26.07.2017 в 19:29
source

1 answer

1

From Angular, you must prepare the sending of the body of the request in the format you are specifying, by default it is sent to you as JSON.

public getListStudents() {
    const url = 'http://localhost/obtener_alunos.php';

    const body = new URLSearchParams();
    body.set('op', 1);

    const headers = new Headers();
    headers.append('Accept', 'application/json');
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    return this.http.post(url, body, { headers });
}

From the PHP server, collect in json as indicated in the comments and respond as you are doing.

$request = json_decode(file_get_contents("php://input"))
...
die(json_encode($vec_res));
    
answered by 26.07.2017 в 22:48