Webservice PHP does not work!

1

I'm developing a system that will use a webservice in PHP and the structure that will be called to execute this webservice is as follows:

<?php

header("Content-Type: application/json; charset=utf-8") ;

  require_once(  "Core.class.php" ) ;
  require_once( "Nomes.class.php" ) ;

    $acao = "";
    $core = new Core( ) ;
    $nome = new Nomes( $core ) ;

    if(isset($_POST['acao'])){
        $acao = $_POST['acao'];

        switch ($acao) {
            case "R":
            $lista = $nome->search( ) ;
            echo json_encode( $lista ) ;

                break;
        }
    }

?>

I'm testing on Postman by putting "action" on the parameter and "R" for value, but nothing returns. Before I put the if(isset($_POST['acao'])){$acao = $_POST['acao']; that I will use to create a CRUD, it was working normally, only after that it stopped working. Does anyone know what it could be?

    
asked by anonymous 28.10.2017 / 06:10

1 answer

0

According to the question you apparently are not sending the content-type (HTTP) header in the request for your webservice. It is necessary that in the postman you send the header content-type: application/x-www-form-urlencoded so that the information sent can be placed in the super global $_POST (eventually you can use multipart / form-data). Basically the request header should have at least this:

POST /a.php HTTP/1.1
Host: localhost
content-type: application/x-www-form-urlencoded

acao=R

With this modified code from your:

<?php

header("Content-Type: application/json; charset=utf-8") ;

  //require_once(  "Core.class.php" ) ;
  //require_once( "Nomes.class.php" ) ;

    $acao = "";
    //$core = new Core( ) ;
    //$nome = new Nomes( $core ) ;

    if(isset($_POST['acao'])){
        $acao = $_POST['acao'];

        switch ($acao) {
            case "R":
            $lista = ['alguma coisa'] ;
            echo json_encode( $lista ) ;

            break;
        }
    }

?>

You get the answer:

HTTP/1.1 200 OK
Host: localhost
Connection: close
Content-Type: application/json; charset=utf-8

["alguma coisa"]
    
28.10.2017 / 18:20