doujingxi3356 2017-03-14 21:53
浏览 57
已采纳

将JSON Array的数据输出为HTML

I'm trying to create an overlay for my twitch stream, which display not only the recent follower but a certain amount of them in chronological order.

Here a visual representation:

latest follower bottom

(Styling is done with HTML/CSS)

I've already managed to get the JSON but I don't know how to echo the names for each ['follows']['user']['display_name']

This is the API: https://api.twitch.tv/kraken/channels/kazutode/follows?limit=5&offset=0&client_id=nht9j8w0u4xazpm1fdc2fmkrqvoici

Here's my code so far (It's not alot, basically nothing xD)

<?php

$clientID = "nht9j8w0u4xazpm1fdc2fmkrqvoici";
$channel = "kazutode";
$limit = 5;
$offset = 0;

$response = json_decode(file_get_contents('https://api.twitch.tv/kraken/channels/'.$channel.'/follows?limit='.$limit.'&offset='.$offset.'&client_id='.$clientID), true);

?>

I know, I know. I should use curl but I don't know how so I stick to json_decode.

Can someone teach me how to get the wanted data out of the ['follows'] array?

  • 写回答

1条回答 默认 最新

  • dongxi1965 2017-03-14 21:59
    关注

    You need a foreach loop. Here is some PHP+HTML mixed to give you an idea:

    $table = '';
    $table .= '<table>';
    foreach($response['follows'] as $entry) {
        $table .= '<tr>';
        $table .= '<td>'.$entry['created_at'].'</td>';
        $table .= '<td>'.$entry['user']['display_name'].'</td>';
        $table .= '<td><a class="your-custom-class" href="'.$entry['_links']['self'].'">View channel</a></td>';
        $table .= '</tr>';
    }
    $table .= '</table>';
    
    echo $table;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?