Webservice PHP does not work!

Asked

Viewed 45 times

1

I’m developing a system that will use a webservice in PHP and the structure that will be called to run this webservice is the following:

<?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 am testing in Postman putting "action" in the parameter and "R" 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 giving results. Anyone knows what could be?

1 answer

0

According to the question apparently you are not sending the header (from the HTTP protocol) content-type in the request to your webservice. It is necessary that in 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 code modified 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;
        }
    }

?>

It is obtained as an answer:

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

["alguma coisa"]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.