我正在尝试制作一个简单的函数,该函数将采用提交的 64 位 steamid 并使用 Steam API 吐回信息。我选择使用 XML 而不是 JSON,因为我过去使用过一些 XML。我对php还是很陌生。
获取数据并打印它工作正常,但我在打印特定字段时遇到问题
$steamid = $_SESSION['steamid'];
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=X&steamids=$steamid&format=xml";
$data = file_get_contents($url);
$xml = simplexml_load_string($data);
print var_dump($xml);
//trying to display the "personaname" but doing it wrong
echo $xml->response->players->player->personaname;
请注意,在此处发布之前删除了我的 API 密钥。这是我使用“print var_dump($xml)”返回的示例。
object(SimpleXMLElement)#1 (1) { ["players"]=> object(SimpleXMLElement)#2 (1) { ["player"]=> object(SimpleXMLElement)#3 (16) { ["steamid"]=> string(17) "76561198001776632" ["communityvisibilitystate"]=> string(1) "3" ["profilestate"]=> string(1) "1" ["personaname"]=> string(5) "johan" ["lastlogoff"]=> string(10) "1368300096" ["commentpermission"]=> string(1) "1" ["profileurl"]=> string(38) "http://steamcommunity.com/id/mrbbqlol/" ["avatar"]=> string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/b7/b765fa514b9c44376e84754acb12e66821d4564c.jpg" ["avatarmedium"]=> string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/b7/b765fa514b9c44376e84754acb12e66821d4564c_medium.jpg" ["avatarfull"]=> string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/b7/b765fa514b9c44376e84754acb12e66821d4564c_full.jpg" ["personastate"]=> string(1) "1" ["primaryclanid"]=> string(18) "103582791431859033" ["timecreated"]=> string(10) "1223663039" ["loccountrycode"]=> string(2) "SE" ["locstatecode"]=> string(2) "26" ["loccityid"]=> string(5) "43754" } } }
在用户 hakre 的帮助下,我打印了原始 XML,它看起来像这样:
<response>
<players>
<player>
<steamid>76561198001776632</steamid>
<communityvisibilitystate>3</communityvisibilitystate>
<profilestate>1</profilestate>
<personaname>johan</personaname>
<lastlogoff>1368300096</lastlogoff>
<commentpermission>1</commentpermission>
<profileurl>http://steamcommunity.com/id/mrbbqlol/</profileurl>
<avatar>
http://media.steampowered.com/steamcommunity/public/images/avatars/b7/b765fa514b9c44376e84754acb12e66821d4564c.jpg
</avatar>
<avatarmedium>
http://media.steampowered.com/steamcommunity/public/images/avatars/b7/b765fa514b9c44376e84754acb12e66821d4564c_medium.jpg
</avatarmedium>
<avatarfull>
http://media.steampowered.com/steamcommunity/public/images/avatars/b7/b765fa514b9c44376e84754acb12e66821d4564c_full.jpg
</avatarfull>
<personastate>1</personastate>
<primaryclanid>103582791431859033</primaryclanid>
<timecreated>1223663039</timecreated>
<loccountrycode>SE</loccountrycode>
<locstatecode>26</locstatecode>
<loccityid>43754</loccityid>
</player>
</players>
</response>
我看过其他类似的问题,但我无法解决我的问题。我应该如何解析和回显特定字段?可以使用 simplexmlelement 吗?
谢谢