2

Preciso puxar a tabela de classificação do futebol: posição, brasão, nome do time, pontos, jogos, vitorias, saldo de gols. Estou puxando de uma API, veja o código PHP que tentei:

<?php
header("Content-Type: application/json");
$jsonData = file_get_contents("http://json.gazetaesportiva.com/brasileiro-serie-a-2019.json");
$arrData = json_decode($jsonData); // Transforma o seu JSON
// print_r($arrData)

echo "<pre>";
print_r($arrData);
echo "</pre>";

print_r ($arrData->grupos);

foreach($obj2 as $key => $value){

   $tier = $value[0]->nome;
   $division = $value[0]->entries[0]->division;
   
}
?>

Esse é parte do resultado:

[17] => stdClass Object
                        (
                            [equipe] => stdClass Object
                                (
                                    [id] => 13
                                    [nome] => Chapecoense
                                    [abreviacao] => CHA
                                    [pais] => Brasil
                                    [imagem] => stdClass Object
                                        (
                                            [url] => https://img-apifutebol.gazetaesportiva.com/uploads/equipe/imagem/chapecoense.png
                                            [thumb] => stdClass Object
                                                (
                                                    [url] => https://img-apifutebol.gazetaesportiva.com/uploads/equipe/imagem/thumb_chapecoense.png
                                                )

                                        )

                                    [imagem_foi_obtida_via_url] => 0
                                    [slug] => chapecoense
                                    [pagina_site] => chapecoense
                                )

                            [pontos] => 9
                            [jogos] => 12
                            [vitorias] => 2
                            [empates] => 3
                            [derrotas] => 7
                            [gols_pro] => 11
                            [gols_contra] => 20
                            [saldo_gols] => -9
                            [aproveitamento] => 25
                            [penalidades] => 0
                        )

                    [18] => stdClass Object
                        (
                            [equipe] => stdClass Object
                                (
                                    [id] => 1510
                                    [nome] => CSA
                                    [abreviacao] => CSA
                                    [pais] => Brasil
                                    [imagem] => stdClass Object
                                        (
                                            [url] => https://img-apifutebol.gazetaesportiva.com/uploads/equipe/imagem/csa.png
                                            [thumb] => stdClass Object
                                                (
                                                    [url] => https://img-apifutebol.gazetaesportiva.com/uploads/equipe/imagem/thumb_csa.png
                                                )

                                        )

                                    [imagem_foi_obtida_via_url] => 0
                                    [slug] => csa
                                    [pagina_site] => csa
                                )

                            [pontos] => 7
                            [jogos] => 12
                            [vitorias] => 1
                            [empates] => 4
                            [derrotas] => 7
                            [gols_pro] => 3
                            [gols_contra] => 20
                            [saldo_gols] => -17
                            [aproveitamento] => 19
                            [penalidades] => 0
                        )

                    [19] => stdClass Object
                        (
                            [equipe] => stdClass Object
                                (
                                    [id] => 7
                                    [nome] => Avaí
                                    [abreviacao] => AVA
                                    [pais] => Brasil
                                    [imagem] => stdClass Object
                                        (
                                            [url] => https://img-apifutebol.gazetaesportiva.com/uploads/equipe/imagem/avai.png
                                            [thumb] => stdClass Object
                                                (
                                                    [url] => https://img-apifutebol.gazetaesportiva.com/uploads/equipe/imagem/thumb_avai.png
                                                )

                                        )

                                    [imagem_foi_obtida_via_url] => 0
                                    [slug] => avai
                                    [pagina_site] => avai
                                )

                            [pontos] => 5
                            [jogos] => 12
                            [vitorias] => 0
                            [empates] => 5
                            [derrotas] => 7
                            [gols_pro] => 5
                            [gols_contra] => 16
                            [saldo_gols] => -11
                            [aproveitamento] => 13
                            [penalidades] => 0
                        )

                )

        )

)
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: obj2 in C:\wamp\www\esporte\index.php on line <i>13</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0320</td><td bgcolor='#eeeeec' align='right'>387000</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\esporte\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: Invalid argument supplied for foreach() in C:\wamp\www\esporte\index.php on line <i>13</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0320</td><td bgcolor='#eeeeec' align='right'>387000</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\esporte\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
</table></font>

Estou com dificuldades em deixar apenas os dados, e alterar a posição e formatação deles. Desde já agradeço.

2
  • o que vc especificamente está tentando gerar??? seria somente a tabela de classificação??
    – Oliveira
    30/07/2019 às 20:59
  • @Oliveira Sim, somente a tabela de classificação.
    – jose113
    31/07/2019 às 21:22

1 Resposta 1

7

No seu cenário ao usar $arrData = json_decode($jsonData); é convertido o json em um objeto e vc tentou percorrer esse objeto de forma equivocada, observe que os dados estão em atributos desse objeto, sendo que alguns atributos podem ser objetos ou arrays. Para pegar os dados da classificação é necessário acessar o atributo grupos que é um array, e na posição 0, que é um objeto, vc pega o atributo classificacao que é um array de objetos.

Utilize o var_dump() para exibir os dados.

$url = "http://json.gazetaesportiva.com/brasileiro-serie-a-2019.json";
$data = file_get_contents($url);
$arrData = json_decode($data);
$title = utf8_decode($arrData->nome);
$ano = utf8_decode($arrData->ano);
$arrClassificacao = $arrData->grupos[0]->classificacao;

var_dump($arrData->grupos[0]->classificacao);

Com relação a classificacão, um exemplo seria:

<?php

$url = "http://json.gazetaesportiva.com/brasileiro-serie-a-2019.json";
$data = file_get_contents($url);
$arrData = json_decode($data);

$title = utf8_decode($arrData->nome);
$ano = utf8_decode($arrData->ano);
$arrClassificacao = $arrData->grupos[0]->classificacao;

?>
<!doctype html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

        <title><?=$title?></title>
    </head>
    <body>
        <h1><?=$title?></h1>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th scope="col">#</th>
                        <th scope="col">Classificação</th>
                        <th scope="col">P</th>
                        <th scope="col">J</th>
                        <th scope="col">V</th>
                        <th scope="col">E</th>
                        <th scope="col">D</th>
                        <th scope="col">GP</th>
                        <th scope="col">GC</th>
                        <th scope="col">SG</th>
                        <th scope="col">%</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                foreach ($arrClassificacao as $key => $equipes) { ?> 
                    <tr>
                        <th scope="row"><?php echo ++$key;?></th>
                        <td>
                            <img src="<?php echo $equipes->equipe->imagem->thumb->url;?>" alt="<?php utf8_decode($equipes->equipe->nome);?>">&nbsp
                            <?php echo utf8_decode($equipes->equipe->nome);?>
                        </td>
                        <td><?php echo $equipes->pontos;?></td>
                        <td><?php echo $equipes->jogos;?></td>
                        <td><?php echo $equipes->vitorias;?></td>
                        <td><?php echo $equipes->empates;?></td>
                        <td><?php echo $equipes->derrotas;?></td>
                        <td><?php echo $equipes->gols_pro;?></td>
                        <td><?php echo $equipes->gols_contra;?></td>
                        <td><?php echo $equipes->saldo_gols;?></td>
                        <td><?php echo $equipes->aproveitamento;?></td>
                    </tr>
            <?php } ?>
                </tbody>
            </table>
       </div>
    </body>
</html>
1
  • 1
    Era isso mesmo! Muito obrigado! Me ajudou muito!
    – jose113
    1/08/2019 às 18:22

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .