List all the data of an API in numerical list (Array)

-2

I have this Array:

[tracks] => Array
        (
            [data] => Array
                (
                    [0] => Array
                        (
                            [title] => Amiga Da Minha Mulher
                            [artist] => Array
                                (
                                    [name] => Seu Jorge
                                )
                        )

                    [1] => Array
                        (
                            [title] => Tá Vendo Aquela Lua
                            [artist] => Array
                                (
                                    [name] => Exaltasamba
                                )
                        )

The number of numbers after ['data'] is the number of songs, or varies depending on the playlist. I put only two as example, but in the API link you can see examples with everything.

In "tracks" has the song list + artist name of each song. How to list them like this:

01 - My Girlfriend's Friend - Seu Jorge

02 - Seeing That Moon - Exaltasamba

And so it goes ...

I try to do everything, but it always makes mistakes.

    
asked by anonymous 14.11.2018 / 09:02

1 answer

1

What you can do is

  • Make an API request using PHP cURL
  • Transform JSON into an Object
  • Filter the requested data (in case you want the title of the song and the author)
  • Mounting an HTML with this data using <ul><li></li></ul>
  • Note: You only need to put the URL of the API in the variable $ urlAPI

    Code:

    <?php
    
    $urlAPI = 'URL_API';
    
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        /**
         * Observação: coloque sempre a opção 'CURLOPT_SSL_VERIFYPEER' como true, por questões de segurança.
         * Coloquei como false apenas para responder a resposta, pois precisava configurar o certificado na minha máquina.
         */
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_URL => $urlAPI
    ));
    
    $dadosAPI = curl_exec($curl);
    curl_close($curl);
    
    $dadosAPI = json_decode($dadosAPI);
    $dataTracks = $dadosAPI->tracks->data;
    
    ?>
    
    <!DOCTYPE html>
    <html dir="ltr" lang="pt-BR"> 
        <head>
        <meta charset="UTF-8" />
            <style type="text/css">
                ul {
                    counter-reset: section;
                    list-style-type: none;
                }
    
                li::before {
                    counter-increment: section;
                    content: counters(section,".") " ";
                }
            </style>
        </head>
        <body>
            <ul id="list"></ul>
        </body>
        <script>
            var ul = document.getElementById("list");
            <?php
                foreach($dataTracks as $data)
                {
            ?>
                    var li = document.createElement("li");
                    li.appendChild(document.createTextNode(' - <?php echo $data->title; ?> - <?php echo $data->artist->name; ?>'));
                    ul.appendChild(li);
            <?php
                }
            ?>
        </script>
    </html>
    
        
    17.11.2018 / 19:36