PHP - POST Requisição Json

4 respostas Resolvido
JulianoJFX

Boa tarde Senhores,

Estou com um código que em sua requisição recebe uma resposta em JSON, a parte da requisição esta perfeita, porem não consigo pegar os elementos do JSON para encpasular em uma string ou variavel.

$data = array(

"PropertyName"=> "IdMessage",
"Condition"=> "Contains",
"Value"=> "537552"

);

$data_string = json_encode($data);
$data_string = “[”.$data_string."]";

$header = array();

$header[] = ‘accept: application/json’;

$header[] = ‘Content-type: application/json-patch+json’;

$header[] = 'Authorization: '.$Authorization;

$ch = curl_init(‘<a href="http://integration.systemsatx.com.br/Tracking/Message/List">http://integration.systemsatx.com.br/Tracking/Message/List</a>’);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Length: 105534’));

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

     
    
    $response = curl_exec ($ch);
    
    // Takes raw data from the request

   
      // Decoding JSON data
        $decodedData = 
        json_decode($response, true);
        
        
       
       
        
        var_dump($decodedData);

?>

Arquivo retornado

array(1) {

[

0

]=>

array(23) {

[

“IdMessage”

]=>

int(537552)

[

“MessageCode”

]=>

string(4) “K9_1”

[

“MessageDirection”

]=>

int(2)

[

“IdWorkFunction”

]=>

NULL

[

“IdOperationalFunction”

]=>

NULL

[

“RuleIntegrationCode”

]=>

NULL

[

“TrackedUnit”

]=>

string(7) “EVU8C11”

[

“IdTrackedUnit”

]=>

int(3801)

[

“TrackedUnitIntegrationCode”

]=>

NULL

[

“APPIntegrationCode”

]=>

NULL

[

“TripCode”

]=>

NULL

[

“SendDate”

]=>

string(20) “2021-10-28T19:42:25Z”

[

“ReceiveDate”

]=>

string(24) “2021-10-28T19:42:27.433Z”

[

“ReadDate”

]=>

NULL

[

“SenderPersonName”

]=>

string(25) “OLIVALDO MOREIRA DA SILVA”

[

“SenderPersonIntegrationCode”

]=>

NULL

[

“ReaderPersonName”

]=>

NULL

[

“ReaderPersonIntegrationCode”

]=>

NULL

[

“Text”

]=>

string(7) “Manobra”

[

“Description”

]=>

NULL

[

“Lat”

]=>

float(-3.77614)

[

“Long”

]=>

float(-38.6047)

[

“Attributes”

]=>

NULL

}

}

array(1) {

[

0

]=>

string(9) “IdMessage”

}

Já criei um importador por arquivo Excel cujo usavamos uma LIB em java e encapsulava as String com o metodo rs.getString e por ai conseguia fazer as inserts…

4 Respostas

JulianoJFX

Já conclui esta etapa, este forum é ativo??? ninguem respondeu, e ainda por cima vão usar o meu codigo!

Lucas_Camara

Basta ver os posts que verá que é ativo.

Calma sô. Fazer uma requisição para obter um json é algo que acha fácil na Internet, não tem pq “usar” o seu código para isso.


Vamos ao problema (o brabo é que não manjo de php, mas vou tentar). Essa parte:

// Decoding JSON data
$decodedData = json_decode($response, true);

Ele não está fazendo o que deveria? Após o decode, vc não consegue navegar nos elementos do json? É isso?

O valor de $response está sendo retornado corretamente?

Lucas_Camara

Ah sim, agora que li melhor e vi que vc conseguiu resolver. O que era o problema?

JulianoJFX
Solucao aceita

kkkk, é facil sim…

Eu não conseguia navegar nos elementos, mas já resolvi a metafora… como retorna uma resposta no formato json eu só consigo acessar os elementos dentro de um, for, while ou foreach…

Criado 29 de outubro de 2021
Ultima resposta 3 de nov. de 2021
Respostas 4
Participantes 2