好的,这是我第一次在这个网站上发帖。我需要一些帮助,我正在尝试将json API中的数据添加到html表中。这是我到目前为止所做的。
<?php
$gamertag = 'x0--ii';
$jsonurl = 'https://tusticles.com/psn/api?psn='.$gamertag;
$json = file_get_contents($jsonurl);
var_dump(json_decode($json));
<?
这是我的输出:
object(stdClass)#1 (2) { ["status"]=> string(3) "200" ["response"]=> object(stdClass)#2 (5) { ["onlineId"]=> string(6) "x0--II" ["avatar"]=> string(77) "http://static-resource.np.community.playstation.net/avatar_m/SCEI/I0053_m.png" ["plus"]=> int(0) ["aboutMe"]=> string(0) "" ["trophies"]=> object(stdClass)#3 (6) { ["trophyLevel"]=> int(3) ["progress"]=> int(7) ["platinum"]=> int(0) ["gold"]=> int(2) ["silver"]=> int(6) ["bronze"]=> int(19) } } }
我想显示图像以及所有值。谢谢你的帮助!
答案 0 :(得分:0)
<?php
$gamertag = 'x0--ii';
$jsonurl = 'https://tusticles.com/psn/api?psn='.$gamertag;
$json = file_get_contents($jsonurl);
#var_dump(json_decode($json, true));
/* json formatted for readability
object(stdClass)#1 (2) {
["status"]=> string(3) "200"
["response"]=> object(stdClass)#2 (5) { // response is an array()
["onlineId"]=> string(6) "x0--II"
["avatar"]=> string(77) "http://static-resource.np.community.playstation.net/avatar_m/SCEI/I0053_m.png"
["plus"]=> int(0)
["aboutMe"]=> string(0) ""
["trophies"]=> object(stdClass)#3 (6) { // trophies is an array()
["trophyLevel"]=> int(3)
["progress"]=> int(7)
["platinum"]=> int(0)
["gold"]=> int(2)
["silver"]=> int(6)
["bronze"]=> int(19)
}
}
}
*/
$json = json_decode($json, true);
$rsp = $json['response']; // response array
$trophies = $rsp['trophies']; // trophy array
// examples
$avatar = $rsp['avatar'];
$onlineId = $rsp['onlineId'];
$trophylevel = $trophies['trophyLevel'];
// inline html example
echo("<div><img src=\"".$rsp['avatar']."\" /></div>");
// or
echo("<div><img src=\"".$json['response']['avatar']."\" /></div>");
echo("<div>".$json['trophies']['progress']."</div>");
// or
echo("<div>".$json['response']['trophies']['progress']."</div>");
?>
答案 1 :(得分:0)
您可以将json_decode数据放入PHP变量中,并像普通的PHP对象数组一样访问它
这是一个简单的例子:
<?php
$gamertag = 'x0--ii';
$jsonurl = 'https://tusticles.com/psn/api?psn='.$gamertag;
$json = file_get_contents($jsonurl);
$content = json_decode($json);
echo "ID: ".$content->response->onlineId."<br />";
echo '<img src="'.$content->response->avatar.'" />';
echo "<table border=1>";
foreach ($content->response->trophies as $key => $value){
echo "<tr>
<td>".$key."</td>
<td>".$value."</td>
</tr>";
}
echo "</table>";
?>
这将循环你的奖杯把它放到HTML表格中。