Php 将spotify json文件转换为html
我正在尝试将spotify json文件转换为html表。然而,这个json文件有点复杂。我在某个地方犯了一个错误,但无法发现我做错了什么。正确的代码是什么Php 将spotify json文件转换为html,php,html,json,spotify,Php,Html,Json,Spotify,我正在尝试将spotify json文件转换为html表。然而,这个json文件有点复杂。我在某个地方犯了一个错误,但无法发现我做错了什么。正确的代码是什么 <?php $spotifylist = "http://spotifycharts.com/api/?type=regional&country=nl&recurrence=daily&date=latest&limit=200"; $contents = file_get_conte
<?php
$spotifylist = "http://spotifycharts.com/api/?type=regional&country=nl&recurrence=daily&date=latest&limit=200";
$contents = file_get_contents($spotifylist);
$decoded = json_decode($contents,true);
$results = $decoded->entries[0]->items;
echo "<table class='chart'> <thead><tr class='row2'><th class='dw'></th><th class='song'>Artiest</th><th class='song'>Titel</th></tr></thead><tbody>";
foreach($results as $entry){
$artist = $entry->track->artists->name;
$name = $entry->track->name;
$x = $entry + 1;
$color = ($x%2 == 0)? 'row2': 'row1';
echo "<tr class='$color'>";
echo "<td class='dw'>". $x ."</td>";
echo "<td class='song'>". $artist ."</td>";
echo "<td class='song'>". $name ."</td>";
echo "</tr>";
}
echo "</tbody></table>";
?>
您的代码有点混乱。为了使它能够工作,我对它进行了一些清理,但我认为您需要更仔细地观察它——它从提要中提取非常精确的数据片段,这可能不安全
工作代码如下:
<?php
$spotifylist = "http://spotifycharts.com/api/?type=regional&country=nl&recurrence=daily&date=latest&limit=200";
$contents = file_get_contents($spotifylist);
$decoded = json_decode($contents);
$results = $decoded->entries->items;
echo "<table class='chart'> <thead><tr class='row2'><th class='dw'></th><th class='song'>Artiest</th><th class='song'>Titel</th></tr></thead><tbody>";
$counter = 0;
foreach($results as $entry){
++$counter;
$artist = (string)$entry->track->artists[0]->name;
$name = $entry->track->album->name;
$color = ($counter % 2 == 0) ? 'row2': 'row1';
echo "<tr class='$color'>";
echo "<td class='dw'>$counter</td>";
echo "<td class='song'>". $artist ."</td>";
echo "<td class='song'>". $name ."</td>";
echo "</tr>";
}
echo "</tbody></table>";
?>
您测试过这个吗?除了thead之外,我的浏览器中没有输出任何表数据。当然,我对它进行了测试。为了确保我刚才再次测试了它,得到了与以前相同的输出:它在phpfiddle中工作,因此代码是正确的。然后我必须找出为什么它在我的网站上不起作用。我很高兴它起作用了!请将答案标记为正确,以便其他人也能受益。如果您有服务器配置问题,也许可以在SuperUser上发布一个新问题?如果我的网站上也有完整的代码,我接受您的回答。还有一件事,我不清楚。然而,我的意思是单曲的名字,而不是专辑的名字。