Página 1 de 1

PHP Como retornar dados

Enviado: 23 Mai 2021 22:07
por rossine
Olá,

Estou testando o PHP e estou enviando dados via "Curl" para outro arquivo .php e preciso retornar dados para o arquivo que enviou os dados.

Algo assim:

envia.php

Código: Selecionar todos

<?php

$iniciar = curl_init();

$dados = array(
   'cTabela' => 'Teste',
   'cCampo'  => 'nome'
);

$dadosArray = json_encode($dados);

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
$headers[] = 'Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$optArray = array(
   CURLOPT_URL            => 'http://localhost/classes/post.php',
   CURLOPT_PORT           => 8080,
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_SSL_VERIFYPEER => false,
   CURLOPT_CUSTOMREQUEST  => 'POST',
   CURLOPT_HTTPHEADER     => $headers,
   CURLOPT_POSTFIELDS     => $dadosArray,
);

$response = curl_setopt_array( $iniciar, $optArray );

$return["body_array"] = curl_exec($iniciar);
$return["body"]       = json_decode($return["body_array"], true );
$return["http"]       = curl_getinfo($iniciar);
$return["error"]      = curl_error($iniciar);

echo '<pre>';

var_dump( $return["body_array"] );
var_dump( $return["body"] );
var_dump( $return["http"] );
var_dump( $return["error"] );

curl_close( $iniciar );

?>

post.php

Código: Selecionar todos


<?php

$rec = json_decode( file_get_contents( "php://input", 1 ) ) ;

return $rec->cTabela;
Como posso recuperar dentro do envia.php o return do post.php ?

PHP Como retornar dados

Enviado: 25 Mai 2021 09:07
por ANDRIL
rossine escreveu:Como posso recuperar dentro do envia.php o return do post.php ?
Já tentou trocar return $rec->cTabela; por die($rec->cTabela);. Voce testou para ver se na página post.php esta tendo o retorno desejado? Pode estar retornado algo vazio por isso acha que não tem retorno.

PHP Como retornar dados

Enviado: 28 Mai 2021 09:01
por rossine
Olá Andril,

Fiz um "echo" e retornou corretamente:

Código: Selecionar todos


<?php

$rec = json_decode( file_get_contents( "php://input", 1 ) ) ;

echo $rec->cTabela;

?>

Obriagdo,