5
Good morning folks! My question is this. I created a foreach to access the data below an API
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$headers = array('Authorization: Token ' . $token);
$ch_subs = curl_init();
curl_setopt($ch_subs, CURLOPT_URL, 'https://dominio.site.com/api/listar');
curl_setopt($ch_subs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_subs, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch_subs, CURLOPT_HTTPHEADER, $headers);
$obj_json_trans = json_decode(curl_exec($ch_subs));{
"total": 1,
"eventos": [
{
"id": "1",
"status": "em andamento",
"titulo": "Evento Demo",
"descricao": "Apenas um evento de teste",
"logomarca_hotsite": "http://",
"logomarca_evento": "http://",
"data_inicio": "10/02/2018",
"data_termino": "11/02/2018",
"participantes": {
"totais": 10,
"confirmados": "5",
"nao_confirmados": "3",
"desativados": "2"
},
"link_hotsite": "http://",
"link_evento": "http://",
"link_inscricao": "http://",
} ] } $eventos = $obj_json_trans->eventos;
foreach ( $eventos as $e )
{
echo "id: $e->id - status: $e->status - título: $e->titulo<br>";
}
curl_close($ch_subs);
I can print the arrays but I don’t know how to print the "attendees" sub array. Does anyone know how to do so I can also print the sub array
"participantes": {
"totais": 10,
"confirmados": "5",
"nao_confirmados": "3",
"desativados": "2"
},
See the full code
<?php
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$headers = array('Authorization: Token ' . $token);
$ch_subs = curl_init();
curl_setopt($ch_subs, CURLOPT_URL, 'https://site.com/api/listar');
curl_setopt($ch_subs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_subs, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch_subs, CURLOPT_HTTPHEADER, $headers);
$obj_json_trans = json_decode(curl_exec($ch_subs));
/* print_r($obj_json_trans); */
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
$eventos = $obj_json_trans->eventos;
foreach ( $eventos as $chave=>$e )
{
echo "id: $e->id - status: $e->status - título: $e->titulo <br> ";
}
curl_close($ch_subs);
?>
</body>
</html>
Participants are missing the [ ' square parentheses to place them as vector. Next, you do the same process of the events and you should print... What do you say?
– White
In the api I’m pulling it looks like the model below { "total": 1, "events": [ { "id": "1", "status": "in progress", "title": "Demo Event", "Description": "Just a test event", "logotag_hotsite": "http://", "logotag_event": "http://", "start_date": "10/02/2018", "data_termino": "11/02/2018", "participants": { "totals": 10, "confirmed": "5", "nao_confirmed": "3", "disabled": "2" }, "link_hotsite": "http://", "link_event": "http://", "link_registration": "http://", } ] }
– leodario
@Leodario edit your question and put your code there, otherwise it becomes difficult to understand.
– João Martins
I cloned the full code
– leodario