Calling class that generates JSON

Asked

Viewed 68 times

0

Good morning! I am breaking my head to recover the Class that generates JSON, I have the following class name apiBusca.php

 public function enviarRequisicao() {                
    $url = 'API';

    $json = new StdClass();
    $json->Chave = $this->Chave;
    $json->Senha = $this->Senha;
    $json->TipoViagem = $this->TipoViagem;
    $json->Companhias = $this->Companhias;
    $json->Adultos = $this->Adultos;
    $json->Criancas = $this->Criancas;
    $json->Bebes = $this->Bebes;
    $json->Trechos = $this->Trechos;
    $data = json_encode($json);                

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $result = curl_exec($ch);

    curl_close($ch);

    return $result;
}

In the api.php data display file I put it as follows:

$chave = "MINHA_CHAVE";
$senha = "MINHA_SENHA";
$tipoviagem = 1;
$companhias = array("GOL");
$adultos = 1;
$criancas = 0;
$bebes = 0;
$trechos = array(array("Origem" => "$Origem", "Destino" => "$LugarDestino", "DataIda" => "$DataIda", "DataVolta" => "$dataVolta"));
$objeto = new apibuscamilhas($chave, $senha, $tipoviagem, $companhias, $adultos, $criancas, $bebes, $trechos);
$ida = new apibuscamilhas();
$ida->Chave = "MINHA_CHAVE";
$ida->Senha = "MINHA_SENHA";
$ida->TipoViagem = 0;
$ida->Companhias = array("GOL");
$ida->Adultos = 1;
$ida->Criancas = 0;
$ida->Bebes = 0;
$ida->Trechos = array(array("Origem" => "$Origem", "Destino" => "$LugarDestino", "DataIda" => "$DataIda"));

Doubt I can’t display it except through var_dump($ida->send());

Could someone help me?

Thank you

  • Utilize CURLOPT_RETURNTRANSFER as true to return the data and give a echo json_encode($ida); in the api.php

  • I added the CURLOPT_RETURNTRANSFER thus curl_setopt($ch, CURLOPT_RETURNTRANSFER, $data); and in the api.php file it returned me only {}

  • Added the echo json_encode.... too? The file api.php have to print a value for then, the file apiBusca.php recover.

  • Yes I did and you didn’t call me back!

No answers

Browser other questions tagged

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