-5
Hail to you guys, I’m starting in the Webservice world and I’ve come across the following problem.
The TOTVS Server via Rest, returns me exactly as follows:
$resposta = enviaConteudoParaAPI($cabecalho, $conteudo, $url, $tpRequisicao);
print_r($resposta);
Upshot:
{"CAMPOS":"[\"\"A1_COD\",\"A1_NOME\",\"TIPO\"]","DADOS":"[[\"00932221\",\"PEDRO GONCALVES\",\"TITULAR\"][\"00164577\",\"JORGE ARAGAO\",\"TITULAR\"]]"}
//TRATADO
$array = json_decode($resposta, true);
$campos = $array['CAMPOS'];
$dados = $array['DADOS'];
print_r($array);
Upshot:
Array ( [CAMPOS] => [""A1_COD","A1_NOME","TIPO"] [DADOS] => [["00932221","PEDRO GONCALVES","TITULAR"]["00164577","JORGE ARAGAO","TITULAR"]] )
It turns out that I need to assign each content to a variable, that is, I need to work with each value differently:
$A1_COD = ['$A1_COD'];
$A1_NOME = ['A1_NOME'];
$TIPO = ['TIPO'];
echo "Codigo: $A1_COD, Nome: $A1_NOME, Tipo: $TIPO";
Where I need the following result:
Codigo: 00932221 , Nome: PEDRO GONCALVES, Tipo: TITULAR
Codigo: 00164577 , Nome: JORGE ARAGAO, Tipo: TITULAR
In other words, I need to transform this array:
Array ([["00932221","PEDRO GONÇALVES","TITULAR"]["00164577","JORGE ARAGAO","TITULAR"]])
In this array:
Array ( [A1_COD] => 00932221 [A1_NOME] => PEDRO GONÇALVES [TIPO] => TITULAR )
Array ( [A1_COD] => 00164577 [A1_NOME] => JORGE ARAGAO [TIPO] => TITULAR )
SOLUTION:
I requested correction in the server response, which was returning with extra quotes, return validated:
$jsonString = '{
"CAMPOS": ["A1_COD", "A1_NOME", "TIPO"],
"DADOS": [
["33436681", "MARCOS ALAN", "REPRESENTANTE"],
["34007644", "MARCOS ALBERTO", "TITULAR"],
["67762840", "MARCOS ALVES", "TITULAR"],
["55178561", "MARCOS ANTONIO", "TITULAR"]
]
}';
$array = json_decode($jsonString, true);
foreach ($array['DADOS'] as $dados) {
$novoArray[] = array_combine($array['CAMPOS'], $dados);
}
echo "<pre>";
print_r($novoArray);
Array
(
[0] => Array
(
[A1_COD] => 33436681
[A1_NOME] => MARCOS ALAN
[TIPO] => REPRESENTANTE
)
[1] => Array
(
[A1_COD] => 34007644
[A1_NOME] => MARCOS ALBERTO
[TIPO] => TITULAR
)
[2] => Array
(
[A1_COD] => 67762840
[A1_NOME] => MARCOS ALVES
[TIPO] => TITULAR
)
[3] => Array
(
[A1_COD] => 55178561
[A1_NOME] => MARCOS ANTONIO
[TIPO] => TITULAR
)
)
Wallace, I’ll try to be more specific.
– Marcos Santos
If you are going to make some consideration, edit your question and add details, but I already say that you should not let someone answer your question and then say clearly what you need.
– Wallace Maxters
Wallace, I’ve edited my doubt, see if it’s clearer.
– Marcos Santos
@Marcossantos but the answer does exactly what you want. You tested the solution I put?
– Wallace Maxters
tested, but not compiled.
– Marcos Santos
What do you mean "not compiled"? PHP is an interpreted language.
– Wallace Maxters
It did not run because php is not accepting the array assigned to the $return variable as a valid array, so nothing is returned.
– Marcos Santos
@Tabs and which error message specifies?
– Guilherme Nascimento
William, good morning. No mistake, just the page is blank.
– Marcos Santos
Wallace, good morning! Dude, your solution is right, what was wrong was the return I was getting from the server, was with extra quotes. Thank you very much for your support.
– Marcos Santos