Class to create json

Asked

Viewed 55 times

0

I made this class in order to create a JSON to return in my Web Service, but it’s not working, someone would know why?

Class Uteis

<?php

class Uteis {
    public static function criarjson($mensagem,$value){

       $json_str = '{"autenticado":$value, "mensagem":$mensagem}';
       $json = json_decode($json_str);
       return json_encode($json);   

    }
}
?>

Index.php

$app->post('/login',function(Request $request,Response $response){
    $allquery = $request->getQueryParams();
    $user = new Usuario();
    $sql = new Sql();
    $autenticado = $user->login($allquery['login'],$allquery['senha']);
    if ($autenticado) {
        $response = Uteis::criarjson("AA",true);
    }else{
        $response = Uteis::criarjson("Erro ao autenticar",false);
    }
    return $response;
});
$app->run();

1 answer

1


Your array can be like this:

<?php

  class Uteis {
    public static function criarjson($mensagem,$value){

      $json_str  = array(
                        "autenticado" => $value,
                        "mensagem"    => $mensagem
                    );
       echo json_encode($json_str );   

    }
}
?>

Browser other questions tagged

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