0

我正在尝试制作一个简单的函数,该函数将采用提交的 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 吗?

谢谢

4

1 回答 1

2

可以使用 simplexmlelement 吗?

是的,它可以而且你已经在使用它了。然而,有问题的部分是您需要首先了解 XML 结构。

如果您在浏览器中进行调试,以下是 simplexml 的快速帮助:

$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);

header('Content-Type: text/xml');
die($xml->asXML());

第一部分实际上就是你的代码,我只是在下面添加了两行,将 XML 逐字逐句地告诉浏览器它是 XML。然后,您的浏览器将按原样显示 XML(通常格式很好,您可以打开/关闭部分,打印漂亮,语法用不同的颜色突出显示,例如在 Chromium 中)。

您可以访问基本 SimpleXML 使用示例中概述的元素。您不能太信任print_rvar_dump依赖 SimpleXMLElements,因为它们不会向您展示整个 XML(子)树。

于 2013-05-11T21:47:57.983 回答