1
Good night,
I am consuming a webservice and get the data in json with Curl. I have the data stored in the variable $data. What I need is this:
Let’s assume that the variable $data has the following data:
$data = [ {"nome":"Edileuza","CPF":"009876543-00"}{"nome":"Cleuza","CPF":"123456789-00"} ]
So as you can see I have the CPF inside that array. I need to consume another webservice that takes the number and brings other information. And with this information I will have another array that I want to put inside the first array.
I’m not able to think of a way to do this but I think I should foreach the $date variable. Below I will show what I think would be right and if anyone can help me I will be grateful.
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
curl_close($ch);
$data = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($data));
$data = json_decode($data, true);
This is where I don’t know what to do
foreach ($data as $key => $value) {
$cpf = $value["CPF"]
$link2 = "https://www.siteexemplo.com.br/ws/$cpf"
$ch2 = curl_init($link2);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
$data2 = curl_exec($ch2);
curl_close($ch2);
$data2 = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($data2));
$data2 = json_decode($data2, true);
}
the result would be
$data2 = [ {"Endereco":"Rua 1"}]
Dai would like the Address to go to the $data array to look like this:
$data = [ {"nome":"Edileuza","CPF":"009876543-00", "Endereco":"Rua 1"}{"nome":"Cleuza","CPF":"123456789-00", "Endereco":"Rua 2"}
Thanks for your help.
Where does "Street 2" come from? From what you’ve shown, the $data2 only returns "Street 1".
– Sam
Hello sam, the result Street 1 comes from the consultation, the second webservice makes the query 1 by one. Then at the end I passed the result of the foreach loop that would appear Street 1 and Street 2
– Rodrigo Pereira de Freitas
In this case it would be something like this: array_push($data, result of the $data2 foreach) and continue the Loop until you go through the whole array
– Rodrigo Pereira de Freitas
Use as follows: https://pastebin.com/dK8TVpFa
– Valdeir Psr
Valdeir, thank you for your help. Your code seems to be the solution but there is no foreach in it. Why do I have to go through the $data array by picking up the index numbers, it’s over 300.
– Rodrigo Pereira de Freitas
I’ll try something here and put it on after
– Rodrigo Pereira de Freitas