我正在尝试创建一个简单的函数,它将使用Steam API提交64位steamid并吐出信息。我选择使用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吗?
由于
答案 0 :(得分: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中)。
您可以访问basic SimpleXML usage examples中列出的元素。您不能完全信任SimpleXMLElements上的print_r
或var_dump
,因为它们不会显示整个XML(子)树。